1

I'm new to Mongo and struggling to retrieve a specific Sub Document of a specific Document

Given the following collection, I want to retrieve the content where "id" = 1 and "data.id" = 2. So, I'd want my result to be the "Java" object.

{
    "id" : "1",
    "title" : "Section1",
    "data" : [ 
        {
            "id" : "1",
            "Product" : "Scala",
            "Description" : "",
        }, 
        {
            "id" : "2",
            "Product" : "Java",
            "Description" : "",
        }, 
        {
            "id" : "3",
            "Product" : "Ruby",
            "Description" : "",
        }, 
        {
            "id" : "4",
            "Product" : "HTML5",
            "Description" : "",
        }
        ]
}

{
    "id" : "2",
    "title" : "Section2",
    "data" : [ 
        {
            "id" : "4",
            "Product" : "Ansible",
            "Description" : "a free software platform for configuring and managing computers",
        }, 
        {
            "id" : "1",
            "Product" : "Chef",
            "Description" : "an open source configuration management tool",
        }, 
        {
            "id" : "2",
            "Product" : "Puppet",
            "Description" : "an open-source tool for managing the configuration of computer systems",
        }, 
        {
            "id" : "3",
            "Product" : "Saltstack",
            "Description" : "open source configuration management and remote execution application",
        }
   ]
}

I'm executing a query like db.myCollection.find({$and : [{"id":"1"},{"data.id":"2"}]}) but this returns the whole document with an id of "1" rather than just the subdocument I'm interested in.

Am I asking Mongo to do something that it's not designed for, or is it just my syntax that is at fault?

DaveH
  • 7,187
  • 5
  • 32
  • 53
  • To do that, you are going to need to use projections. you can find some examples here: https://docs.mongodb.org/v3.0/reference/operator/aggregation/project/ – jpgrassi Dec 15 '15 at 10:58
  • Possible duplicate of [Retrieve only the queried element in an object array in MongoDB collection](http://stackoverflow.com/questions/3985214/retrieve-only-the-queried-element-in-an-object-array-in-mongodb-collection) – styvane Dec 15 '15 at 14:29

4 Answers4

3

Maybe this will help:

db.myCollection.find({"id":"1","data.id":"2"},{"id":1,"data.$":1})

Its not required to pass $and

Sarath Nair
  • 2,828
  • 2
  • 21
  • 36
2

You can use $match in conjunction with $unwind. These two are a part of aggregation framework.

The answer to your question is :

db.collection.aggregate([{$unwind:'$data'},{$match:{'data.Product':{$eq:'Java'}}}]);

Refer: MongoDb unwind operator

harshavmb
  • 3,404
  • 3
  • 21
  • 55
2

You can use aggregation framework.

  1. Retrieve document with matching "data" item.

    db.getCollection('yourColl').aggregate([ {$unwind:"$data"}, {$match:{"id":"1", "data.id":"2"}}, {$group:{_id:"$_id", "id":{$first:"$id"}, "title":{$first:"$title"}, "data":{$push:"$data"}}} ])

  2. If you only want get specific subdocument

    db.getCollection('yourColl').aggregate([ {$unwind:"$data"}, {$match:{"id":"1", "data.id":"2"}}, {$project:{_id:0, "id":"$data.id", "product":"$data.Product", "Description":"$data.Description"}} ])

hecnabae
  • 407
  • 4
  • 21
0

Answered my own question :

db.myCollection.find({$and : [{"id":"1"},{"data.id":"2"}]},{"data.$":1})

DaveH
  • 7,187
  • 5
  • 32
  • 53