0

I'm having some trouble with querying a Mongo Collection.

I have a Collection like this:

{
"_id" : "555bd34329de3cf232434ef2",
"cars" : [ 
    {
        "0" : {
            "parts" : [ 
                {
                    "name" : "x1",
                    "price" : 12
                },
                {
                    "name" : "x2",
                    "price" : 14
                }
            ]
        },
        "1" : {
            "parts" : [ 
                {
                    "name" : "y1",
                    "price" : 8
                },
                {
                    "name" : "y2",
                    "price" : 12
                }
            ]
        } 
    }
]
}

I'd like to return just the following:

"parts" : [ 
    {
        "name" : "x1",
        "price" : 12
    },
    {
        "name" : "x2",
        "price" : 14
    }
]

In other words, I need to figure out how to query the Collection by two parameters at the same time:

  • where the ID matches "555bd34329de3cf232434ef2"

  • where the "name" of the part matches "x1"

Does anyone know how to do this kind of nested query?

  • Don't hesitate to create multiple collections, you could have a collection Parts, Cars and GroupsOfCars. It is a good practice adviced by Meteor: https://guide.meteor.com/collections.html#schema-design And it makes the query easier – Gaëtan Rouziès Feb 07 '18 at 12:26
  • That "0" and "1" inside your "cars" array... Should that be array indices or are those really fields in a single nested document that you keep inside your "cars" array? – dnickless Feb 07 '18 at 12:43
  • @dnickless The "0" and "1" are array indices – Nick Andrews Feb 07 '18 at 12:44
  • It looks like you don't need the `cars` array, doesn't it? The `parts` array could be at the document root, or at least that's what appears from your code. That would simplify things. – Miguel Calderón Feb 07 '18 at 12:47
  • There are other fields at the document root that I have excluded for the sake of example. In any case, I agree it's best that I move "cars" to a separate Collection. – Nick Andrews Feb 07 '18 at 12:51

1 Answers1

0

Assuming a document structure like this:

{
    "_id" : ObjectId("555bd34329de3cf232434ef2"),
    "cars" : [ 
        {
            "parts" : [ 
                {
                    "name" : "x1",
                    "price" : 12
                }, 
                {
                    "name" : "x2",
                    "price" : 14
                }
            ]
        }, 
        {
            "parts" : [ 
                {
                    "name" : "y1",
                    "price" : 8
                }, 
                {
                    "name" : "y2",
                    "price" : 12
                }
            ]
        }
    ]
}

you can run the following query:

db.collection.find({ "_id": ObjectId("555bd34329de3cf232434ef2"), "cars.parts.name" : "x1" }, { "_id": 0, "cars.$": 1 })

which will get you pretty close to where you want to be:

{
    "cars" : [ 
        {
            "parts" : [ 
                {
                    "name" : "x1",
                    "price" : 12
                }, 
                {
                    "name" : "x2",
                    "price" : 14
                }
            ]
        }
    ]
}

You could get closer using the aggregation framework if that's not good enough...

dnickless
  • 10,733
  • 1
  • 19
  • 34