I have created a Nodejs API server which is accessing AWS MongoDB(version: 3.6). When I call one API (say api/lowest) it gives me perfect result without any error. But when I call this API multiple times almost simultaneously (15 API calls), for few API calls(3-7) it gives
MongoError: Cursor Not found, cursor id xxxxx
API code is as follows -
app.get('/lowest', async (req, res, next) => {
let url = 'mongodb://url';
let param1 = req.query.param1;
let param2 = req.query.param2;
let client = mongodb.MongoClient;
try {
client.connect(url, function (err, client) {
let db = client.db("db1");
let collection = db.collection('collection1');
let options = {
allowDiskUse: false,
timeout: false,
noCursorTimeout: true
};
// returns mongodb aggregation pipeline
let pipeline = helper.getLowestPipeline(param1, param2);
//Step 1: declare promise
let myPromise = () => {
return new Promise((resolve, reject) => {
collection.aggregate(pipeline, options).toArray(function (err, data) {
err
? reject(err)
: resolve(data);
});
});
};
//Step 2: async promise handler
let callMyPromise = async () => {
let result = await (myPromise());
client.close();
//anything here is executed after result is resolved
return result;
};
//Step 3: make the call
callMyPromise().then(function (result) {
res.send(JSON.stringify({"status": 200, "error": null, "response": result}));
});
});
} catch (e) {
client.close();
res.json({"status": 500, "error": e, "response": null});
res.end();
}
});
pm2 monit result is as following :
MongoError: Cursor Not found, cursor id: 7820213409290816 in namespace: db_name.collection_name
at Connection. (/rc19_pricemongo/node_modules/mongodb/lib/core/connection/pool.js:466:61)
at Connection.emit (events.js:210:5)
at processMessage (/rc19_pricemongo/node_modules/mongodb/lib/core/connection/connection.js:364:10)
at TLSSocket. (/rc19_pricemongo/node_modules/mongodb/lib/core/connection/connection.js:533:15)
at TLSSocket.emit (events.js:210:5)
at addChunk (_stream_readable.js:309:12)
at readableAddChunk (_stream_readable.js:290:11)
at TLSSocket.Readable.push (_stream_readable.js:224:10)
at TLSWrap.onStreamRead (internal/stream_base_commons.js:182:23) {
ok: 0,
errmsg: 'Cursor Not found, cursor id: 7820213409290816 in namespace: db_name.collection_name', code: 43,
name: 'MongoError',
[Symbol(mongoErrorContextSymbol)]: { isGetMore: true }
I am not able to identify the problem, any suggestion will be helpful...