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

问题描述 投票:1回答:1

如何查询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对象的文档。

mongodb mongodb-query
1个回答
0
投票

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

db.collection.find({}, { "00:50:56:84:c8:1b": 1 })
© www.soinside.com 2019 - 2024. All rights reserved.