Mongodb querying documents

I recently started working on mongodb which is very new for me, Currently I have a confusion on the document querying command “$elemMatch”. I used this tag for selecting documents greater than 10 and less than or equal to 20, and the result it gives, but if I didn’t specify the $elemMatch tag then the output is displayed but the document which didn’t satisfy the condition are also displayed. So can anyone help me in understanding what $elemMatch exactly does ?

@Wwe_Star

This seems weird!!

I tested this and works fine in my environment.

Let me show you the result.

replset:PRIMARY> db.inventory.find()
{ "_id" : ObjectId("66124b3e7d01624ec29e6444"), "instock" : [ { "qty" : 10 } ] }
{ "_id" : ObjectId("66124b507d01624ec29e6445"), "instock" : [ { "qty" : 120 } ] }
{ "_id" : ObjectId("66124b567d01624ec29e6446"), "instock" : [ { "qty" : 500 } ] }
{ "_id" : ObjectId("66124bcc7d01624ec29e6447"), "instock" : [ { "qty" : 5 } ] }

replset:PRIMARY> db.inventory.find({"instock.qty":{$gt:10, $lte:120}});

Output:
{ "_id" : ObjectId("66124b507d01624ec29e6445"), "instock" : [ { "qty" : 120 } ] }

replset:PRIMARY> db.inventory.find({ instock: { $elemMatch: { qty: { $gt: 10, $lte: 500 } } } })

Output:

{ "_id" : ObjectId("66124b507d01624ec29e6445"), "instock" : [ { "qty" : 120 } ] }

{ "_id" : ObjectId("66124b567d01624ec29e6446"), "instock" : [ { "qty" : 500 } ] }

The $elemMatch operator matches documents that contain an array field with at least one element that matches all the specified query criteria.

Exactly, what MongoDB version you are using ?