Watch keynotes and sessions from MongoDB.live, our virtual developer conference.

Query using $expr doesn't return expected result

Insert Query -
db.product1.insertOne({ “_id” : 24, “pno” : “P320”, “pname” : “Juicer”, “price” : 5000, “qty” : 44, “MRP” : 10000 });

Expression query - db.product1.find({$expr:{$gte:[“price”,“MRP”]}});

Im expecting above document to be returned since price is greater that 10000. But there is no output. Please suggest.

In general, when you want the value of a field you need to prefix its name with the dollar sign. Otherwise the field name is used as the value or your operator.

In your find() the strings price and MRP are compared. You should use $price and $MRP to indicate that you want the value.