1

How do i query mongo for the list of all non-private blogs along with the private blogs of currently logged-in user.

Blog(collection):
_user_id: ref(User), title: String, body: String, private: Boolean, default:false

I can get all the non-private blogs with this query:

Blog.find({_user_id: req.user}).where('private', false).exec();

But I also want to get all the blogs which are marked private only by this current logged-in user.

Is this thing even possible using single query. Do I have to rely on advance mongodb features like map-reduce / aggregate .

2
  • Have you looked at using $or? Commented Sep 7, 2014 at 19:22
  • Yes, but I don't think that would help here cos I want both conditions. Commented Sep 7, 2014 at 19:37

1 Answer 1

1

You can use $or to include both cases in a single query:

Blog.find({$or: [
    // Non-private blogs
    {private: false},
    // Blogs of the current user
    {_user_id: req.user}
]}).exec(function(err, docs) { ... });

This will provide a union of the results of the two $or clauses.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.