Mitul Parmar
Mitul Parmar

Reputation: 13

Mongodb query to search all nested objects of array is empty

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

Upvotes: 1

Views: 847

Answers (1)

R2D2
R2D2

Reputation: 10737

Maybe something like this:

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

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

playground

Upvotes: 1

Related Questions