how to use golang mongodb driver query $or with $and?

1.1k Views Asked by At

1.judge field label_status is exists, if exists is true find one record. 2.if field label_status exist is false. find one record label_status eq 1 and author
eq sample

i use the code:

bson.D{
        {"$or", bson.D{{"label_status", bson.D{{"$exists", false}}}}},
        {"$and", bson.A{bson.M{"label_status": Labeling}, bson.M{"annotator": "sample"}}},
    }
1

There are 1 best solutions below

0
On
err := collection.FindOne(context.Background(), bson.D{
        {"$or", bson.D{{"label_status", bson.D{{"$exists", false}}}}},
        {"$and", bson.A{bson.M{"label_status": Labeling}, bson.M{"annotator": "sample"}}},
    }).Decode(&result)