Home > Mobile >  Mongodb query to search all nested objects of array is empty
Mongodb query to search all nested objects of array is empty

Time:03-21

I am trying to achieve one query for below data.

[
  {
    "blocks": [
      {
        "issues": {}
      },
      {
        "issues": {
          "dt": "dt"
        }
      }
    ]
  },
  {
    "blocks": [
      {
        "issues": {
          "ext": "data"
        }
      }
    ]
  },
  {
    "blocks": [
      {
        "issues": {}
      }
    ]
  }
]

So, Here are case

from any documents, from any blocks, if any issues object is not empty then skip that document. If blocks having all issues are empty object then return that document.

I tried @elementMatch, $eq, $nin, $ne, @all etc way but not able to solve.

any help would be great

CodePudding user response:

Maybe something like this:

 db.collection.find({
 $nor: [
   {
    "blocks": {
      $elemMatch: {
        "issues": {
          $nin: [
            {}
          ]
        }
      }
    }
  }
]
})

Explained: Search documents where there is only blocks.issues: {}

playground

  • Related