Bill
Bill

Reputation: 5150

Mongo Agregate: Set new field to true/false if an array has values or not

Here I'm trying to make a new field called imADogOwner and if dogNames has no values in the array set the field to false, if there are values in the array set it to true

{
    "imADogOwner": { $dogNames: true, $ne: [] }
}

Upvotes: 1

Views: 45

Answers (1)

mickl
mickl

Reputation: 49945

You need the aggregation version of $ne:

db.collection.aggregate([
    {
        $addFields: { imADogOwner: { $ne: [ "$dogNames", [] ] } }
    }
])

Upvotes: 1

Related Questions