I have a collection having following data:
{"_id" : ObjectId("5220222f8188d30ce85d61cc"),
"testfields" : [{ "test_id" : 1, "test_name" : "xxxx" }] }
when I query :
db.testarray.find({ "testfields" : { "$type" : 4 } })
it returns no data,but same returns data when I do:
db.testarray.find({ "$where" : "Array.isArray(this.testfields)" })
It returns data, do the type:4 identifies some other kind of list?
Because
testfields
is anArray
,$type : 4
will perform the "is array" check against every element intestfields
as opposed totestfields
itself. Since yourtestfields
contains just oneObject
, it does not get returned.If on the other hand you inserted the following into your collection,
it would get returned because now one of the elements of
testfields
is anArray
.More info explaining this in the docs.