Created
November 18, 2011 08:07
-
-
Save samyakbhuta/1375876 to your computer and use it in GitHub Desktop.
The importance ( presence + consequence ) of null in mongodb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Consider this as base collection. It is important to know how the MongoDB interprets 'null'. | |
| > db.items.find(); | |
| { "_id" : ObjectId("4ec60c984f5c08053610772f"), "attr1" : "a" } | |
| { "_id" : ObjectId("4ec60cb14f5c080536107730"), "attr1" : "b" } | |
| { "_id" : ObjectId("4ec60cb44f5c080536107731"), "attr1" : "c" } | |
| { "_id" : ObjectId("4ec60d0c4f5c080536107732"), "attr2" : "d" } | |
| { "_id" : ObjectId("4ec60d444f5c080536107733"), "attr2" : "d", "attr1" : null } | |
| { "_id" : ObjectId("4ec60fc34f5c080536107734"), "attr1" : "" } | |
| // Do a query, call it query A | |
| > db.items.find({attr1:null}); | |
| { "_id" : ObjectId("4ec60d0c4f5c080536107732"), "attr2" : "d" } | |
| { "_id" : ObjectId("4ec60d444f5c080536107733"), "attr2" : "d", "attr1" : null } | |
| // Do an another query, call it query B | |
| > db.items.find({attr1:{$type:10}}); | |
| { "_id" : ObjectId("4ec60d444f5c080536107733"), "attr2" : "d", "attr1" : null } | |
| // Do an another query, call it query C | |
| > db.items.find({attr1:{$exists:false}}); | |
| { "_id" : ObjectId("4ec60d0c4f5c080536107732"), "attr2" : "d" } | |
| As you can see the result of query B and C combined together will be the result of query A. | |
| In another words B (union) C = A . Also B (intersection) C = 0. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment