仅使用Pymongo从MongoDB找到特定键

发布于 2025-01-31 07:19:54 字数 1564 浏览 1 评论 0 原文

我已经编写了此代码,只想检查一个名为 virtual_machines 的键。我有以下形式的数据。我只想检查 virtual_machines 是否在那里,请不要再次上传数据。

  {
          "Virtual_Machines": {
               "Debian": {
                    "VM_Name": "Debian",
                    "VM_Location": "eastus",
                    "VM_Disk_Name": "Debian_OsDisk_1_b890f5f5c42647549c881c0706b85201",
                    "VM_Publisher_Info": {
                         "publisher": "debian",
                         "offer": "debian-11",
                         "sku": "11-gen2",
                         "version": "latest"
                    },
                    "Vm_Disk_Type": "Standard_D2s_v3",
                    "VM_Encryption": null
               },
               "Ubuntu": {
                    "VM_Name": "Ubuntu",
                    "VM_Location": "eastus",
                    "VM_Disk_Name": "Ubuntu_disk1_0610e0fde49b481490ef0a069a03b460",
                    "VM_Publisher_Info": {
                         "publisher": "canonical",
                         "offer": "0001-com-ubuntu-server-focal",
                         "sku": "20_04-lts-gen2",
                         "version": "latest"
                    },
                    "Vm_Disk_Type": "Standard_D2s_v3",
                    "VM_Encryption": null
               }
          }
     },

我的代码是,但是它将输出作为

db = client['Audit']
vms = db['virtual_machine']
vm = json.dumps(vm)
vmachine = vms.find_one({"Virtual_Machine"},{'_id':0})    

print(vmachine)

I have written this code and I want to check only for a key named Virtual_Machines. I have data in the following form. I only want to check if Virtual_Machines is there then don't upload data again.

  {
          "Virtual_Machines": {
               "Debian": {
                    "VM_Name": "Debian",
                    "VM_Location": "eastus",
                    "VM_Disk_Name": "Debian_OsDisk_1_b890f5f5c42647549c881c0706b85201",
                    "VM_Publisher_Info": {
                         "publisher": "debian",
                         "offer": "debian-11",
                         "sku": "11-gen2",
                         "version": "latest"
                    },
                    "Vm_Disk_Type": "Standard_D2s_v3",
                    "VM_Encryption": null
               },
               "Ubuntu": {
                    "VM_Name": "Ubuntu",
                    "VM_Location": "eastus",
                    "VM_Disk_Name": "Ubuntu_disk1_0610e0fde49b481490ef0a069a03b460",
                    "VM_Publisher_Info": {
                         "publisher": "canonical",
                         "offer": "0001-com-ubuntu-server-focal",
                         "sku": "20_04-lts-gen2",
                         "version": "latest"
                    },
                    "Vm_Disk_Type": "Standard_D2s_v3",
                    "VM_Encryption": null
               }
          }
     },

My code is but it is giving output as None.

db = client['Audit']
vms = db['virtual_machine']
vm = json.dumps(vm)
vmachine = vms.find_one({"Virtual_Machine"},{'_id':0})    

print(vmachine)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒解孤独 2025-02-07 07:19:54

find_one()中的第一个参数必须是 dict 。您正在遇到错误,因为您正在传递 {“ virtual_machine”} python将其解释为 set https://docs.python.org/3/library/stdtypes.html#set

If you want to check that a key exists or not ,无论其价值如何,都使用 $存在运算符。

vms.find_one({'Virtual_Machines': {'$exists': True}}, {'_id':0})

The first parameter in find_one() must be a dict. You are getting your error because you are passing {"Virtual_Machine"} which python interprets as a set https://docs.python.org/3/library/stdtypes.html#set

If you want to check that a key exists or not, regardless of its value, use the $exists operator. https://www.mongodb.com/docs/manual/reference/operator/query/exists/

vms.find_one({'Virtual_Machines': {'$exists': True}}, {'_id':0})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文