为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

Go
郎朗坤 2022-01-10 17:47:48
我想使用 $or 查找包含任何给定参数的条目。数据库条目如下所示"resources" : {    "compute" : "compute4",    "storage" : "storage3",    "network" : "network2"},我想找到满足资源内任何字段的条目。bkCollection.Find(bson.M{"resources": bson.M{    "compute": filter.Resources.Compute, "$or",    "storage": filter.Resources.Storage, "$or",    "network": filter.Resources.Network}}).All(&result)
查看完整描述

1 回答

?
慕神8447489

TA贡献1780条经验 获得超1个赞

您需要使用$or运算符构造此 mongo shell 查询的等效项:


db.collection.find({

    "$or": [

        { "resources.compute" : "compute5" },

        { "resources.storage" : "storage3" },

        { "resources.network" : "network1" }

    ]

})

其中的go结构如下:


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)


查看完整回答
反对 回复 2022-01-10
  • 1 回答
  • 0 关注
  • 157 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号