I would like to run the following query in golang using mgo in a pipeline.
{\"key1\" : 1,
\"$or\" : [{\"key2\" : 2}, {\"key3\" : 2}]}
I have l
go lang Mongo db Or query
findQuery := bson.M{"key1" : 1}
orQuery := []bson.M{}
orQuery := append(orQuery, bson.M{"key2" : 2}, bson.M{"key3" : 2})
findquery["$or"] = orQuery
result := []interface{}
err := mongo.DB.C("collectionName").find(findQuery).All(&result)
Your mongo query can be translated to the following:
pipeline := bson.D{
{"key1", 1},
{"$or", []interface{}{
bson.D{{"key2", 2}},
bson.D{{"key3", 2}},
}},
}
The query should be equivalent to the following in the mongo console:
db.mycollection.find({"key1" : 1, "$or" : [{"key2" : 2}, {"key3" : 2}]})
If you'd rather wish to use unordered maps, bson.M
, it would be like this:
pipeline := bson.M{
"key1": 1,
"$or": []interface{}{
bson.M{"key2": 2},
bson.M{"key3": 2},
},
}