Mongo find if any possible field in array of subdocuments has value

Viewed 45

I'm running an aggregate on a Collection and am using a lookup to another child Collection.

Each child may or may not have a field (foo) that can contain a String.

I'd like to project a Boolean for if at least 1 of these fields has a String for any of the child subdocuments.

So I can have something like

db.Parent.aggregate([
  {
    '$lookup': {
      'from': 'Children', 
      'localField': '_id', 
      'foreignField': 'parentId', 
      'as': 'CHILDREN'
    }
  }, {
     childHasFoo: {
         "$CHILDREN" : {$elemMatch: {foo: {$ne: ''}}}
     }
}
]

You can see my projection for childHasFoo . I'm thinking I can use $elemMatch or $anyElementTrue. Remember, foo may not exist on an/all of the children subdocuments either. So that needs to be taken into account.

Thanks!

1 Answers
  • iterate loop of CHILDREN array using $map array operator,
  • check your condition using $ne this will return boolean value
  • $anyElementTrue to check is any element has true
  {
    $addFields: {
      childHasFoo: {
        $anyElementTrue: {
          $map: {
            input: "$CHILDREN",
            in: { $ne: ["$$this.foo", ""] }
          }
        }
      }
    }
  }

Second option to more accurate condition,

  • $trim to remove white space from string
  • $not and $in to check is string empty "" or null then false otherwise true
  {
    $addFields: {
      childHasFoo: {
        $anyElementTrue: {
          $map: {
            input: "$CHILDREN",
            in: {
              $not: {
                $in: [
                  { $trim: { input: "$$this.foo" } },
                  ["", null]
                ]
              }
            }
          }
        }
      }
    }
  }
Related