Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB query to return only embedded document

assume that i have a BlogPost model with zero-to-many embedded Comment documents. can i query for and have MongoDB return only Comment objects matching my query spec?

eg, db.blog_posts.find({"comment.submitter": "some_name"}) returns only a list of comments.

edit: an example:

import pymongo

connection = pymongo.Connection()
db = connection['dvds']

db['dvds'].insert({'title': "The Hitchhikers Guide to the Galaxy",
                   'episodes': [{'title': "Episode 1", 'desc': "..."},
                                {'title': "Episode 2", 'desc': "..."},
                                {'title': "Episode 3", 'desc': "..."},
                                {'title': "Episode 4", 'desc': "..."},
                                {'title': "Episode 5", 'desc': "..."},
                                {'title': "Episode 6", 'desc': "..."}]})

episode = db['dvds'].find_one({'episodes.title': "Episode 1"}, 
                              fields=['episodes'])

in this example, episode is:

{u'_id': ObjectId('...'),
 u'episodes': [{u'desc': u'...', u'title': u'Episode 1'},
               {u'desc': u'...', u'title': u'Episode 2'},
               {u'desc': u'...', u'title': u'Episode 3'},
               {u'desc': u'...', u'title': u'Episode 4'},
               {u'desc': u'...', u'title': u'Episode 5'},
               {u'desc': u'...', u'title': u'Episode 6'}]}

but i just want:

{u'desc': u'...', u'title': u'Episode 1'}
like image 851
Carson Avatar asked Feb 15 '10 20:02

Carson


4 Answers

I think what you wanted is this:

print db.dvds.aggregate([
  {"$unwind": "$episodes"}, # One document per episode
  {"$match": {"episodes.title": "Episode 1"} }, # Selects (filters)
  {"$group": {"_id": "$_id", # Put documents together again
              "episodes": {"$push": "$episodes"},
              "title": {"$first": "$title"} # Just take any title
             }
  },
])["result"]

The output (besides the whitespaces) is:

[ { u'episodes': [ { u'title': u'Episode 1',
                     u'desc': u'...'
                   }
                 ],
    u'_id': ObjectId('51542645a0c6dc4da77a65b6'),
    u'title': u'The Hitchhikers Guide to the Galaxy'
  }
]

If you want to get rid from the u"_id", append the pipeline with:

  {"$project": {"_id": 0,
                "episodes": "$episodes",
                "title": "$title"}
               }
like image 195
H.D. Avatar answered Oct 08 '22 05:10

H.D.


This same question was asked over on the Mongo DB Google Groups page. Apparently its not currently possible but it is planned for the future.

http://groups.google.com/group/mongodb-user/browse_thread/thread/4e6f5a0bac1abccc#

like image 26
Stephen Curran Avatar answered Oct 08 '22 06:10

Stephen Curran


The mongodb javascript shell is documented at http://docs.mongodb.org/manual/reference/method/

If you want to get back only specific fields of an object, you can use

db.collection.find( { }, {fieldName:true});

If, on the other hand, you are looking for objects which contain a specific field, you can sue

db.collection.find( { fieldName : { $exists : true } } );
like image 39
Dominik Avatar answered Oct 08 '22 05:10

Dominik


Match more simple:

db['dvd'].find_one( {'episodes.title': "Episode 1"},{'episodes.title': true} )

Query:

coll.find( criteria, fields );

Get just specific fields from the object. E.g.:

coll.find( {}, {name:true} );

http://www.mongodb.org/display/DOCS/dbshell+Reference

like image 43
Derlok Avatar answered Oct 08 '22 06:10

Derlok