MongoDB CursorNotFound Error on collection.find() for a few hundred small records

前端 未结 2 1587
北恋
北恋 2021-01-14 15:14

I\'m running on Mongo 3.6.6 (on a small Mongo Atlas cluster, not sharded) using the native Node JS driver (v. 3.0.10)

My code looks like this:

const          


        
2条回答
  •  执念已碎
    2021-01-14 15:53

    First of all, if your data is too big it's not a good idea to use toArray() method, instead it's better to use forEach() and loop throw the data. Just like this :

    const records = await collection.find({
      userId: ObjectId(userId),
      status: 'completed',
      lastUpdated: {
        $exists: true,
        $gte: '2018-06-10T21:24:12.000Z'
      }
    });
    
    records.forEach((record) => {
        //do somthing ...
    });
    

    Second, you can use {allowDiskUse: true} option for getting large data.

    const records = await collection.find({
      userId: ObjectId(userId),
      status: 'completed',
      lastUpdated: {
        $exists: true,
        $gte: '2018-06-10T21:24:12.000Z'
      }
    },
    {allowDiskUse: true});
    

提交回复
热议问题