3

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...

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
DEV
  • 2,106
  • 3
  • 25
  • 40
  • 1
    You forgot to remove `rc_price.rc_price_data` from the error message. The error says the cursor was already closed by the time you attempt to retrieve chunk of data. Most of mongo driver functions can return promises to avoid callback hell. Your http handler is already async - try to rewrite your code using awaits instead of callbacks. Also it's recommended to `client.connect` once at the application start. The driver can manage a pool of connections - let it do so https://stackoverflow.com/questions/10656574/how-do-i-manage-mongodb-connections-in-a-node-js-web-application – Alex Blex Dec 18 '19 at 12:24
  • @AlexBlex: thanks for your suggestions, I followed them, Its working properly now. – DEV Dec 19 '19 at 01:56

1 Answers1

0

I recommend you to use the find method, you can pass an object as a second argument that receives a timeout property, it will prevent the cursor to timeout.

IDModel.find({},{ timeout: false })

Documentation: http://mongodb.github.io/node-mongodb-native/3.6/api/Collection.html#find