user1207177
user1207177

Reputation: 577

Find embedded document with one field but not another

I have a collection where each document contains an embedded collection; for example:

{
 cells: [
  {
   x: 1,
   y: 2
  },
  {
   x: 3
  }
 ]
 /* more fields not shown */
}

Is there a way to find those documents that have at least one document in the cells collection without a y-value (like the record shown here)?

Upvotes: 1

Views: 41

Answers (1)

kkkkkkk
kkkkkkk

Reputation: 7748

You could use $elementMatch to achieve it:

db.col.find({
  cells: {
    $elemMatch: {
      y: {
        $exists: false
      }
    }
  }
});

Upvotes: 3

Related Questions