查询MongoDB文档中的特定对象并仅检索该对象

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了查询MongoDB文档中的特定对象并仅检索该对象相关的知识,希望对你有一定的参考价值。

如何查询MongoDB文档中的特定对象并仅检索该对象

文献:

{ "_id" : "mac_table", "00:50:56:84:c8:1b" : { "switch_port" : "ethernet1/37", "mac_ip_routed_by" : "or1-befw02", "mac_switch" : "adsfasfd", "mac_ip" : "ddsfa", "switch_port_description" : "asdfsafsfda", "mac_ip_dns" : "asdfsafd", "switch_port_mode" : "trunk", "mac_vlan:" : "3362" }, "00:50:56:84:c8:1f" : { "switch_port" : "ethernet1/13", "mac_ip_routed_by" : "asdf", "mac_switch" : "dfdsfd", "mac_ip" : "asdfasdf", "switch_port_description" : "adsfasdf", "mac_ip_dns" : "asdfasfd", "switch_port_mode" : "trunk", "mac_vlan:" : "3201" }, "00:50:56:a5:64:f0" : { "switch_port" : "", "mac_ip_routed_by" : "adsfdfsa", "mac_switch" : "", "mac_vlan" : "", "mac_ip" : "adsfasdf", "switch_port_description" : "", "mac_ip_dns" : "adadfsadf", "switch_port_mode" : "asdsadf" }, "18:a9:05:65:43:12" : { "switch_port" : "ethernet116/1/6", "mac_ip_routed_by" : "nvvvvvv", "mac_switch" : "aaana", "mac_ip" : "10.40.77.60", "switch_port_description" : "test", "mac_ip_dns" : "test", "switch_port_mode" : "access", "mac_vlan:" : "76" } }

我只想检索:

"00:50:56:84:c8:1b" : { "switch_port" : "ethernet1/37", "mac_ip_routed_by" : "or1-befw02", "mac_switch" : "adsfasfd", "mac_ip" : "ddsfa", "switch_port_description" : "asdfsafsfda", "mac_ip_dns" : "asdfsafd", "switch_port_mode" : "trunk", "mac_vlan:" : "3362" }

对于我尝试的每个查询,它返回整个文档,我在这个单个文档中有大约30K个对象。如果我错误地构造数据,请告知如何更好地构建数据以获得我需要的查询。我担心添加30K文档和1个带有30k对象的文档。

答案

你应该能够像这样简单地使用projection

db.collection.find({}, { "00:50:56:84:c8:1b": 1 })

以上是关于查询MongoDB文档中的特定对象并仅检索该对象的主要内容,如果未能解决你的问题,请参考以下文章

仅检索 MongoDB 集合中对象数组中的查询元素

从 Firebase 中的特定 ID 列表中检索数据,关注用户并仅获取他们的帖子

Morphia:将MongoDb文档检索为Java对象

MongoDB - 查询匹配数组值的多个文档;

Mongodb查询过滤文档中的嵌套对象数组

多次读取 plist 与创建对象并仅读取 plist 一次以访问 plist 中的数据