Gogo中的MongoDB(golang)与mgo:如何使用逻辑运算符来查询? [英] MongoDB in Go (golang) with mgo: how to use logical operators to query?

查看:207
本文介绍了Gogo中的MongoDB(golang)与mgo:如何使用逻辑运算符来查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  {key1:1, 
$ or:[{key2:2},{key3:2}]}

我到处寻找,但我找不到像这样的例子。例如:

  ... 
pipeline:= [] bson.M {
bson.M {$ match:bson.M {key1:1,
$或:bson.M {key2:2,key3:2},
}
...
}

编译正确找不到任何东西。任何想法?



预先感谢您

解决方案

您的mongo查询可以转换为以下内容:

  pipeline:= bson.D {
{key1,1},
{$ or,[] interface {} {
bson.D {{key2,2}},
bson.D {{key3,2}},
}},
}

查询应该等同于以下内容mongo控制台:

  db.mycollection.find({key1:1,$ or:[{key2 :2},{key3:2}]})

使用无序地图, bson.M ,它会是这样的:

  pipeline:= bson.M {
key1:1,
$或:[] interface {} {
bson.M {key2:2},
bson.M {key3:2},
},
}


I would like to run the following query in golang using mgo in a pipeline.

{"key1" : 1,
 "$or" : [{"key2" : 2}, {"key3" : 2}]}

I have looked everywhere, but I cannot find an example like this. I have tried many different combinations, for example:

...
pipeline := []bson.M{
                     bson.M{    "$match" :  bson.M{ "key1" : 1,  
                                                   "$or" : bson.M{ "key2" : 2, "key3" : 2},
                     }
                     ...
            }

which compiles correctly, does not find anything. Any ideas?

Thank you in advance

解决方案

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},
    },
}

这篇关于Gogo中的MongoDB(golang)与mgo:如何使用逻辑运算符来查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆