Subject: Re: [mongodb-user] Missing field semantics




I have to read it couple of times, but I got it.  As always, thank you so much for your answer.

On Friday, July 14, 2017 at 3:11:49 PM UTC-5, Asya Kamsky wrote: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


On Wed, Jul 5, 2017 at 10:50 PM, Shiv <saaga...@xxxxxxxxx> wrote:

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

--
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...@googlegroups.com.
To post to this group, send email to mongod...@xxxxxxxxxxxxxxxx.
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

--
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 [email protected].
To post to this group, send email to [email protected].
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/388c2fd3-a813-4ec4-90f9-552c46811c51%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



Programming list archiving by: Enterprise Git Hosting