dsedug8644 2016-05-21 14:15
浏览 82
已采纳

查找使用逻辑运算符查询嵌套结构?

I want to find entries that contain any of the given arguments using $or. The database entry looks like below

"resources" : {
    "compute" : "compute4",
    "storage" : "storage3",
    "network" : "network2"
},

I want to find entries that satisfy any of the fields inside resources.

bkCollection.Find(bson.M{"resources": bson.M{
    "compute": filter.Resources.Compute, "$or",
    "storage": filter.Resources.Storage, "$or",
    "network": filter.Resources.Network}}).All(&result)
  • 写回答

1条回答 默认 最新

  • doujiao6116 2016-05-21 14:42
    关注

    You need to construct the equivalent of this mongo shell query using the $or operator:

    db.collection.find({
        "$or": [
            { "resources.compute" : "compute5" },
            { "resources.storage" : "storage3" },
            { "resources.network" : "network1" }
        ]
    })
    

    where in go this would be structured as:

    bkCollection.Find(bson.M{ "$or": []bson.M{ 
        bson.M{ "resources.compute": filter.Resources.Compute }, 
        bson.M{ "resources.storage": filter.Resources.Storage },
        bson.M{ "resources.network": filter.Resources.Network }
    }}).All(&result)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?