About the value passed by the $and operator of mongoDB

db.inventory.find( { $and: [ { price: { $ne: 1.99 } }, { price: {$exists: true } } ] } )

this query selects all documents in the collection inventory, provided that price is not equal to 1.99 and the price field exists. My problem is that I now need to set {$ne: 1.99} after price as a variable instead of writing it out directly. But I found that if:

var obj = {},
db.inventory.find( { $and: [ { price: obj }, { price: { $exists: true } }] } )
The

query statement is invalid. I would like to ask the condition in the query statement must be written directly, can not be put into the variable? Is there any solution?

Mar.09,2021

has solved the problem and should be queried directly using the chained operation of mongoose

Menu