MongoDB query with elemMatch for nested array data

2019-01-23 08:28发布

问题:

I have a document containing nested array data. I tried hopelessly to filter the data using $elemMatch but I cannot figure out why it is not working.

{
'id' : 1,
'name' : 'test',
'modules' : [
    {
        name: 'foo',
        mandatory: false,
        group: [
            {
                name: g1
            }]
    },
    {
        name: 'bar',
        mandatory: false,
        group: [
            {
                name: g2
            }]
    }]
}

I tried using this query:

db.test.find(
{
  modules: {
            $elemMatch: {
                 name: "foo",
            }
  }
}

But it keeps returning all the modules. If I use mandatory: true it returns nothing, which seems to indicates it works. Any idea what am I doing wrong? Thanks!

回答1:

Your query is simply returning all docs that contain a modules element where name == 'foo'. To use $elemMatch to filter the output, you need to use it in the projection argument of the find call instead of part of the query:

db.test.find({}, {modules: {$elemMatch: {name: 'foo'}}})

To combine both concepts, you can reference the index of the array element matched in the query with $:

db.test.find({modules: {$elemMatch: {name: 'foo'}}}, {'modules.$': 1})

Either way returns:

{
  "_id": ObjectId("..."),
  "modules": [
    {
      "name": "foo",
      "mandatory": false,
      "group": [
        {
          "name": "g1"
        }
      ]
    }
  ]
}

If you need other fields included in the output, add them to the projection object (e.g. name: 1).



标签: mongodb