Missing field semantics

53 views
Skip to first unread message

Shiv

unread,
Jul 5, 2017, 10:50:59 PM7/5/17
to mongodb-user

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" }


First question

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

Consider below documents 

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

Asya Kamsky

unread,
Jul 14, 2017, 4:11:49 PM7/14/17
to mongodb-user
When you query, the match semantics can only match a type of something that exists.

“missing” not-really-type was added in aggregation to differentiate between existing fields which are “null” and fields that are missing.

When you query, you can use {“a”:  null } and that will match *both* a being absent (missing) *and* a being present and set to null.

comparison treats both null and non-existent fields as same when comparing against value type ?

In find/match or in aggregation?   

In aggregation, comparing across BSON types missing and null are *not* treated the same.
In find, missing and null are treated the same, unless the query is {$type:null} which will only match a field set to null, and not a missing field (just like {$exists:false} will only match a missing field.

Asya



--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+unsubscribe@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/9c6344cf-b75b-47c5-8fb0-cdc3f403f4fa%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Asya Kamsky
Lead Product Manager
MongoDB
Download MongoDB - mongodb.org/downloads
We're Hiring! - https://www.mongodb.com/careers

Shiv

unread,
Aug 3, 2017, 6:12:55 PM8/3/17
to mongodb-user

I have to read it couple of times, but I got it.  As always, thank you so much for your answer.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages