By default, MongoDb has the $and operator in find query.
For example of this:
db.products.find({$and: [{name:"Mazda 3"}, {manufacturer:"Mazda"}]});
You can shortcut to:
db.products.find({name:"Mazda 3", manufacturer:"Mazda"});
So why still $and operator?
MongoDb builds using Javascript engine, when you search the same fields, the last search criteria will overwrite all. For example below
db.products.find({parts:"Seats", parts:"Doors"});
is equivalent to
db.products.find({parts:"Doors"});
The correct way to search the same fields is using $and operator.
db.products.find({$and:[{parts:"Seats"}, {parts:"Doors"}]});
No comments:
Post a Comment