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

时间:2017-12-21 04:53:30

标签: mongodb mongodb-query

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

1 个答案:

答案 0 :(得分:0)

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

possible = "advapi32.dll,gdi32.dll,imagehlp.dll,kernel32.dll,msvcrt.dll,mswsock.dll,ntdll.dll,ole32.dll,oleaut32.dll"
test = "advapi32.dll,comctl32.dll,comdlg32.dll,gdi32.dll,gdiplus.dll,hal.dll,imagehlp.dll,kernel32.dll,mpr.dll,mscoree.dll,msi.dll,msvcrt.dll,mswsock.dll,ndis.sys,netapi32.dll,ntdll.dll,ntoskrnl.exe,ole32.dll,oleaut32.dll,oledlg.dll,opengl32.dll,psapi.dll,rpcrt4.dll,setupapi.dll,shell32.dll,shlwapi.dll,tapi32.dll,ucc12.dll,user32,user32.dll,wininet.dll,winmm.dll,winspool.drv,ws2_32.dll"

possible_list = possible.split(",")
does_match = []
for t in test.split(","):
    if t in possible_list:
        does_match.append("1")
    else:
        does_match.append("0")
print(",".join(does_match))