If slice exists and if it contains at least one specific variable set to true

Simply use:

pipeline = append(pipeline, bson.M{
    "$match": bson.M{
        "subscriptions": bson.M{"$elemMatch": bson.M{"premium": true}},

No need to check if it exists, it must, else it cannot have an element with premium=true.

And if you have only this one condtion to the element, you may also simplify it to:

pipeline = append(pipeline, bson.M{
    "$match": bson.M{"subscriptions.premium": true},

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top