开发者

How do I do this query in MongoDB?

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});

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜