MongoDB - how to query for a nested item inside a collection? -


i have data looks this:

[     {         "_id" : objectid("4e2f2af16f1e7e4c2000000a"),         "advertisers" : [             {                 "created_at" : isodate("2011-07-26t21:02:19z"),                 "category" : "infinity pro spin air brush",                 "updated_at" : isodate("2011-07-26t21:02:19z"),                 "lowered_name" : "conair",                 "twitter_name" : "",                 "facebook_page_url" : "",                 "website_url" : "",                 "user_ids" : [ ],                 "blog_url" : "",             }, 

and thinking query give id of advertiser:

var start  = new date(2011, 1, 1); > var end  = new date(2011, 12, 12); > db.agencies.find( { "created_at" : {$gte : start , $lt : end} } , { _id : 1 , program_ids : 1 , advertisers { name : 1 }  } ).limit(1).toarray(); 

but query didn't work. idea how can add fields inside nested elements list of fields want get?

thanks!

use dot notation (e.g. advertisers.name) query , retrieve fields nested objects:

db.agencies.find( { "advertisers.created_at" : {$gte : start , $lt : end} } , { _id : 1 , program_ids : 1 , "advertisers.name": 1 }  } ).limit(1).toarray(); 

reference: retrieving subset of fields , dot notation


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -