Suppose I have many "posts" in a database as documents, each with an "author" attribute.
I want to execute a query like this:
posts.find(author IN 3, 5, 733, 144, 66, 3457 , 22, 156, 344...)
Basicaly, I want to find all posts where author is ..... and then give a hund开发者_运维百科red ids.
What's the most efficient way to do this in Mongo?
Use Mongo's $in
operator:
posts.find({"author" : {"$in":[3, 5, 733, 144, 66, 3457 , 22, 156, 344]}});
Here's the reference: http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%24in
You should probably index the author field too: posts.ensureIndex({author:1});
精彩评论