Consider below documents in Mongo - 3.4.1
db.test.insertMany([
{ "_id" : ObjectId("59536363246c629cf4a3ef0e"), "a" : 3 },
{ "_id" : ObjectId("59536363246c629cf4a3ef0f"), "b" : 1 }
])
Below both queries appears to suggest there is "missing" type.
db.test.aggregate({'$project':{"a-absent": {$in: [ {$type:"$a"}, [ "missing" ] ] } }} );
{ "_id" : ObjectId("59536363246c629cf4a3ef0e"), "a-absent" : false },
{ "_id" : ObjectId("59536363246c629cf4a3ef0f"), "a-absent" : true }
db.test.aggregate([{ $project: {"type": {$type: "$a"}}}])
{ "_id" : ObjectId("59536363246c629cf4a3ef0e"), "type" : "double" },
{ "_id" : ObjectId("59536363246c629cf4a3ef0f"), "type" : "missing" }
db.test.find( { "a" : { $type : "double" } } )
works fine.
Can you explain why this is not the case with below queries ?
Both
db.test.find( { "a" : { $type : "missing" } } );
db.test.aggregate( {$match:{ "a" : { $type : "missing" } }} );
returns
Error: error: {
"waitedMS" : NumberLong(0),
"ok" : 0,
"errmsg" : "unknown string alias for $type: missing",
"code" : 2
}
Second question
db.test.insertMany([
{ "_id" : ObjectId("59536363246c629cf4a3ef0e"), "a" : 3 },
{ "_id" : ObjectId("59536363246c629cf4a3ef0f"), "b" : 1 },
{ "_id" : ObjectId("59536363246c629cf4a3ef0a") },
])
Below query appears to work for both null and non-existent field
db.test.aggregate([{ $project: {"compare":{"$gt": ["$a", null]}}}])
returns
{ "_id" : ObjectId("59536363246c629cf4a3ef0e"), "compare" : true },
{ "_id" : ObjectId("59536363246c629cf4a3ef0f"), "compare" : false },
{ "_id" : ObjectId("59536363246c629cf4a3ef0f"), "compare" : false }
which is expected as aggregation can compare across types.
Can you confirm if comparison treats both null and non-existent fields as same when comparing against value type ?
Appreciate all the help.
Thanks,
Shiv