如何编写Mongo查询以查找有条件的子文档 [英] How to write Mongo query to find sub document with condition

查看:58
本文介绍了如何编写Mongo查询以查找有条件的子文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这样的集合中有一个文档,我需要找到带有form_Id:1和Function_Id:2的记录,以及如何编写mongo查询.

I have a document in a collection like this, I need to find the record with form_Id:1 and Function_Id:2, how to write the mongo query.

"Form_Id" : 1,
"Function" : [{
  "Function_Id" : 1,
  "Role" : [{
      "Role_Id" : 1,
      "UserId" : ["Admin", "001"]
    }]
}, {
  "Function_Id" : 2,
  "Role" : [{
      "Role_Id" : 2,
      "UserId" : ["Admin", "005"]
    }]
}]

推荐答案

您可以使用点符号和 $ 位置投影运算符可以做到这一点:

You can use dot notation and the $ positional projection operator to do this:

db.test.find({Form_Id: 1, 'Function.Function_Id': 2}, {_id: 0, 'Function.$': 1})

返回:

{"Function": [{"Function_Id": 2, "Role": [{"Role_Id": 2, "UserId": ["Admin", "005"]}]}]}

这篇关于如何编写Mongo查询以查找有条件的子文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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