我已经编写了这段代码,我只想检查一个名为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
}
}
},我的代码是,但它以None的形式提供输出。
db = client['Audit']
vms = db['virtual_machine']
vm = json.dumps(vm)
vmachine = vms.find_one({"Virtual_Machine"},{'_id':0})
print(vmachine)发布于 2022-05-22 08:05:17
find_one()中的第一个参数必须是dict。您正在获得错误,因为您正在传递{"Virtual_Machine"},python将其解释为set https://docs.python.org/3/library/stdtypes.html#set。
如果要检查键是否存在,无论其值如何,请使用$exists运算符。https://www.mongodb.com/docs/manual/reference/operator/query/exists/
vms.find_one({'Virtual_Machines': {'$exists': True}}, {'_id':0})https://stackoverflow.com/questions/72323776
复制相似问题