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
testfieldsis anArray,$type : 4will perform the "is array" check against every element intestfieldsas opposed totestfieldsitself. Since yourtestfieldscontains 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
testfieldsis anArray.More info explaining this in the docs.