MongoDB NodeJS Native Driver(mongodb) vs Mongo Shell Performance

一世执手 提交于 2021-01-28 11:49:31

问题


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?


回答1:


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; 


来源:https://stackoverflow.com/questions/57789926/mongodb-nodejs-native-drivermongodb-vs-mongo-shell-performance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!