Search code examples
node.jsmongodbmongodb-querymongodb-.net-drivernode-mongodb-native

MongoDB NodeJS Native Driver(mongodb) vs Mongo Shell Performance


I have 10000 records in MongoDB in table1.

Data is as below:

"_id" : ObjectId("5d5e500cb89312272cfe51fc"),
"cities" : [ 
    {
        "cityid" : "5d5d2205cdd42d1cf0a92b33",
        "value" : "XYZ"
    }, 
    {
        "cityid" : "5d5d2214cdd42d1cf0a92b34",
        "value" : "Rowcliffe"
    }, 
],

Query is as below:

      {
        $unwind: "$cities"
      },
      { "$addFields": { "cities.cityid": { "$toObjectId": "$cities.cityid" } } },
      {
        $lookup: {
          from: "cities",
          localField: "cities.cityid",
          foreignField: "_id",
          as: "docs"
        }
      },

So, here i lookup cityid in another table with lookup query in Robo3T and mongo shell. All works fine.

I am getting result in 0.08 sec for 10000 records.

Now, same query m running in nodejs with mongodb native driver, here m getting result in 15 sec.

I'm not getting why this huge difference between this.I don't know what i am doing wrong in nodejs.I have written the same query in nodejs with mongodb native driver.

Please let me know what i m doing wrong.

Why this nodejs mongodb native driver performance is so poor?


Solution

  • In Robo3T there is a default query limit so it takes less time because when it fetches the limit its exit.

    To avoid it you need to add to your query execution this snipped:

    // change the limit size, default 50
    DBQuery.shellBatchSize = 500000;