Amit
Amit

Reputation: 4000

combining conditions in mongodb

I am trying to find all posts made by a user or his frnds...

So far i have this

Activity.any_of({"user_id" => 1}, {"for_id" => 1}, {"comments.user_id" => 1})

now, how do i add to these the condition that user_id can be by anyone who is a frnd of user... will doing this work?

Activity.any_of({"user_id" => 1}, {"for_id" => 1}, {"comments.user_id" => 1}).any_in("user_in" => [1,2])

Upvotes: 0

Views: 270

Answers (1)

Remon van Vliet
Remon van Vliet

Reputation: 18625

Not sure how to do it in rails but the query you're looking for is either :

db.posts.find({$or:[{user_id:1}, {user_id:{$in:[friend_id1, friend_id2,.. friend_idn]}}]})

Or a bit easier, combine your user id and your friend user ids into one list and do

db.posts.find({user_id:{$in:[user_id, friend_id1, friend_id2,.. friend_idn]}})

Upvotes: 1

Related Questions