Mongo Query doubt


#1

What is the difference between these two queries

Posts.findOne({ posted: true, postId: id });

and

Posts.findOne({ $and: [ { posted: true }, {  postId: id } ] });

both seem to be returning same results, when do we use one vs another??


#2

Yeah they’re the same. $and is a mostly unnecessary operator, I’ve never used it. Maybe if you look in the manual you can find some uses for it?


#3

$and is handy when you’re dynamically creating queries.

Imagine a list of todo items, which you can optionally filter, for example only items that are due today. In your server code you can simply check if an optional filter is defined, if so push that query to the $and array.

For static queries it has little use indeed.