Using findOne in a loop takes too long in Node.js

后端 未结 5 492
时光取名叫无心
时光取名叫无心 2020-12-12 07:35

I\'m using Node.js with MongoDB, I\'m also using Monk for db access. I have the below code :

console.time(\"start\");

collection.findOne({name: \"jason\"},         


        
5条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-12 08:14

    Answer for question 1: Yes, you are right.

    Is it because the async nature of Node.js.

    And to prevent that Node.js provides some mechanism for that you can use it otherwise you can do it on your own manually by setting one flag.

    Answer for question 2:

    you can use $in instead of findOne, it will be ease and fast.

    e.g. .find({ "fieldx": { "$in": arr } })

    arr :- In this you need to provide whole array.

提交回复
热议问题