开发者

How do I do this query in MongoDB?

开发者 https://www.devze.com 2023-02-20 17:33 出处:网络
Suppose I have many \"posts\" in a database as documents, each with an \"author\" attribute. I want to execute a query like this:

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

精彩评论

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