TIMEX
TIMEX

Reputation: 271584

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 hundred ids.

What's the most efficient way to do this in Mongo?

Upvotes: 1

Views: 145

Answers (1)

Pablo
Pablo

Reputation: 8644

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

Upvotes: 3

Related Questions