In MongoDB version 2.2 (the next stable release), it will be possible
to use the positional operator to return specific embedded documents
in an array:
https://jira.mongodb.org/browse/SERVER-828
Here is an example from MongoDB 2.2 rc-0 (development release):
sample doc:
{_id: 1, array: [ {id : 507, name: "Jenna"}, {id: 506, name :
"Matt" } ] }
query:
db.collection.find({_id:1, "
array.id":506},{_id:0, "array.$":1})
result:
{ "array" : [ { "id" : 506, "name" : "Matt" } ] }
Information on the positional operator:
http://www.mongodb.org/display/DOCS/Updating/#Updating-The%24positionaloperator
On Aug 1, 3:35 pm, Sergei Tulentsev <
sergei.tulent...@gmail.com>
wrote:
> Yes, you can retrieve a subset of fields using the syntax you mentioned.
> However, you're asking for a bit more. You're asking how to retrieve a
> value of a field of an element of an array, which is something you can't do
> at the moment.
>
> On Wed, Aug 1, 2012 at 10:37 PM, Xiao Yunting <
yunting.x...@gmail.com>wrote:
>
>
>
>
>
>
>
>
>
> > Hi there,
>
> > I set up a collection structure as follows (this is one document in the
> > collection):
>
> > {
> > _id: ddd
> > value_set:
> > [
> > {
> > key_t : 1
> > value_t : aaa
> > },
>
> > {
> > key_t : 2
> > value_t : bbb
> > },
> > {
> > key_t : 3
> > value_t : ccc
> > },
> > ....
>
> > ]
> > }
> > ....
>
> > My question is that whether there is a way to get one specific *value_t*by specifying the
> > *_id* and *value_set.key_t*. Such as *xxx.find({_id : ddd,
> > value_set.key_t: 2},{value_set.value_t:1})*, and then I can get "bbb" as