0

My document looks like:

{ "entity_id" : 2,
  "features" :
  [
     { "10" : "name" },
     { "20" : "description" },
     ... ,
     { "90" : "availability" }
  ]
 }

I would like to, knowing 2 things: the value of "entity_id" (2), and the value of the property in one of the elements of the "features" array, to retrieve only the subdocument

{ "20" : "description" }

in one query. Many thanks!

Michal Gasek
  • 6,173
  • 1
  • 18
  • 20
Alex
  • 210
  • 2
  • 13
  • .find({'features.20':{$exists:true}}) – benathon Jan 29 '14 at 01:19
  • This will give the whole document. I want only the element document where property is 20 – Alex Jan 29 '14 at 01:32
  • http://stackoverflow.com/questions/9289384/select-only-subdocuments-or-arrays – benathon Jan 29 '14 at 01:33
  • @portforwardpodcast Are you trying to point out that "Any query in mongodb always return root document", as stated in the answer to the question in your link? If so, another answer points out that this limitation goes away in 2.2.x – Alex Jan 29 '14 at 01:41
  • The aggregation framework changes the nature of what you can do currently and is not a good substitute for returning just portions of a document. As it's designed to "aggregate", you'll find general usage to be cumbersome for non-aggregation type queries. – WiredPrairie Jan 29 '14 at 02:11

1 Answers1

0

If you want to get only a part of the whole document, use so called Projection operators

See examples below:

> db.collection.find().pretty()
{
    "_id" : ObjectId("52e861617acb7ce761e64a93"),
    "entity_id" : 2,
    "features" : [
        {
            "10" : "name"
        },
        {
            "20" : "description"
        },
        {
            "90" : "availability"
        }
    ]
}

Projection operators are specified in find() like here:

> db.collection.find({},{ features : { $elemMatch : { 20 : { $exists: true } }}}).pretty()
{
    "_id" : ObjectId("52e861617acb7ce761e64a93"),
    "features" : [
        {
            "20" : "description"
        }
    ]
}

> db.collection.find({},{ entity_id : 1, features : { $elemMatch : { 20 : { $exists: true } }}}).pretty()
{
    "_id" : ObjectId("52e861617acb7ce761e64a93"),
    "entity_id" : 2,
    "features" : [
        {
            "20" : "description"
        }
    ]
}
> db.collection.find({},{ _id : 0, entity_id : 1, features : { $elemMatch : { 20 : { $exists: true } }}}).pretty()
{ "entity_id" : 2, "features" : [ { "20" : "description" } ] }

$elemMatch for projection is available in MongoDB since version 2.2.

Hope it solves your problem.

Michal Gasek
  • 6,173
  • 1
  • 18
  • 20
  • None of those return what I'm looking for. Am I missing something? Did you test them? – Alex Jan 29 '14 at 03:54
  • 2
    Maybe you should consider including exactly the output you are expecting in your question if that's not what you meant since there seems to be a confusion? I pasted you the output from MongoDB console together with the queries, so a question "whether I tested these queries" is a bit ridiculous. – Michal Gasek Jan 29 '14 at 10:08
  • From my question: to retrieve the document { "20" : "description" } in one query – Alex Jan 29 '14 at 16:33