go mongo error: The dollar ($) prefixed field '$cond' in 'ready.$cond' is not valid for storage

Viewed 57

I am trying to update my collection using go-mongo driver findOneAndUpdate method.

I am trying to $set the ready field like this

    if update, ok := update["$set"].(bson.M); ok {
        setter["ready"] = getModifier()
    }

My getModifier() looks like this.

func getModifier() bson.M {
    andQuery := []bson.M{{
        "$ne": bson.A{"$catalog_field", nil},
    }}

    andQuery = append(andQuery, bson.M{"$ne": bson.A{"$pricing_field", nil}})

    query := bson.M{
        "$cond": bson.M{  //this is where the error is happening, read further
            "if": bson.M{
                "$and": andQuery,
            },
            "then": true,
            "else": false,
        },
    }

    return query
}

And then I pass the new update to the mongo driver like this, along with other params.

result := collection.FindOneAndUpdate(ctx, filter, update, opts)

When I run this, I get an error saying

The dollar ($) prefixed field '$cond' in 'ready.$cond' is not valid for storage

Why is $cond not valid here?.What am I missing?.

0 Answers
Related