开发者

In MongoDB, how do I query for something similar to Twitter?

开发者 https://www.devze.com 2023-01-23 09:37 出处:网络
Suppose I follow users 4, 7, 10, and 20. How can I query that in Mongo? db.all_posts.find(users = 4, 7, 10, 20).sort( by date )

Suppose I follow users 4, 7, 10, and 20. How can I query that in Mongo?

db.all_posts.find(users = 4, 7, 10, 20).sort( by date )

I don't know the right s开发者_运维知识库yntax. Is this scalable?


Use the $in operator to match against multiple values:

db.all_posts.find({user: {$in: [4, 7, 10, 20]}}).sort({date: 1})

As you can see, the find(), sort() and most other methods in MongoDB take in objects as parameters. These objects are called query expression objects.

The question Making a Twitter-like timeline with MongoDB should answers your question about scalability, as it discusses the same data model as yours.

0

精彩评论

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

关注公众号