Created
January 27, 2011 08:28
-
-
Save gatesvp/798237 to your computer and use it in GitHub Desktop.
An example of using nested docs. Or basically a hash table instead of an array of js objects.
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
| var x = {_id: 1, 'tags' : { 'castle' : { 'n' : 10 }, 'age' : { 'n' : 2 } } }; | |
| var y = {_id: 2, 'tags' : { 'keep' : { 'n' : 2 }, 'castle' : { 'n' : 6 } } }; | |
| var z = {_id: 3, 'tags' : { 'keep' : { 'n' : 5 }, 'cats' : { 'n' : 6 } } }; | |
| db.doc.save(x); | |
| db.doc.save(y); | |
| db.doc.save(z); | |
| // find all docs | |
| db.doc.find(); | |
| // find only docs tagged castle | |
| db.doc.find({'tags.castle' : {$exists:true}}); | |
| // find docs tagged castle, sort by value of 'n' in castle | |
| db.doc.find({'tags.castle' : {$exists:true}}).sort({'tags.castle.n':-1}); | |
| // as above, but return less fields | |
| db.doc.find({'tags.castle' : {$exists:true}}, {_id:1, 'tags.castle':1}).sort({'tags.castle.n':-1}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment