MongoDB in Go (golang) with mgo: how to use logical operators to query?

后端 未结 2 570
北恋
北恋 2021-02-07 19:32

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

2条回答
  •  日久生厌
    2021-02-07 19:49

    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)
    

提交回复
热议问题