Mongodb : $in operator vs lot of single queries

前端 未结 1 766
自闭症患者
自闭症患者 2020-12-01 20:48

I know MongoDB is able to handle a lot of requests/sec, but let\'s say I have to query a lot of documents of a collection given their _id; what sounds better: making a $in o

相关标签:
1条回答
  • 2020-12-01 21:05

    I would definitely go with using the $in query and providing a array of _ids.

    Example:

    db.collection.find({
        "key": {
            "$in": [
                ObjectId("xxx"),
                ObjectId("yyy"),
                ObjectId("zzz")
            ]
        }
    })
    

    Why?

    • If you loop, there is a certain amount of setup and teardown for each query creating and exhausting cursors which would create overhead.
    • If you are not doing this on a local machine it also creates tcp/ip overhead for every request. Locally you could use domain sockets.
    • There is a index on "_id" created by default and collecting a group of documents to return in a batch request should be extremely fast so there is no need to break this up into smaller queries.

    There's some additional documentation here if you want to check it out.

    0 讨论(0)
提交回复
热议问题