I am storing Tweets in a MongoDB database, and would like to use Field Selectors (http://www.mongodb.org/display/DOCS/Querying#Querying-FieldSelection) to return just the text, the date and the screen name of the user.
{
"_id" : NumberLong("213686009408"),
"text" : "RT @Milanello: Lightning over the Donb...",
"created_at" : "Fri, 15 Jun 2012 17:37:38 +0000",
"user" : {
"screen_name" : "xxxxx",
"profile_image_url" : "http://a0.twimg.com/pro[...].jpg"
}
}
Creating a query that returns just the text and date is easy enough:
> db.abc.find({},{created_at:1,text:1})
I’m not sure how to return the user.screen_name property as well?
[UPDATE]
I had previously tried using the dot notation and it threw an exception, however, as per both answers below (I have +1’d both) the dot notation does work, but only if you use “” around the attribute names.
So, this does NOT work:
db.abc.find({},{created_at:1,text:1, user.screen_name:1})
But this DOES:
db.abc.find({},{created_at:1,text:1, "user.screen_name":1})
You can use dot notation as you would in a query:
However, the field will not be flattened, so the result objects still have a
user-subdocument:However, flattening this in code shouldn’t be a big problem.