In $elemMatch as projection, why do we get Objectids with no document

Dear All,
When we use $elemMatch operator as a projection then we get some matching sub documents while some ObjectIDs without any fields. What is the reason for the same, unable to understand from video.

Hi @Niket_Agrawal, interesting question.

I think of it like this:

  1. The first step selects all documents with class_id:431.
  2. This subset is the input for the second part.
  3. _id is always printed, unless _id:0 is indicated.
  4. $elemMatch will only print if there is a match
  5. Then you only see the _id

edit 1
( I can’t find this in the docs, so it’s just an interpretation. )

edit 2
There is a similar example here.

1 Like

Thanks for superb and simple explanation Sir.