$ ne不适用于pymongo

时间:2013-12-17 11:44:00

标签: python mongodb python-2.7 pymongo mongodb-query

我的$ne : ""与pymongo无法正常工作。

hashtable['ACR-WEB-01']=db.logs.find({"host_name" : "WEB-01"}, {"service_description": {"$ne" : ""}}).distinct("service_description")
print hashtable['ACR-WEB-01']

印刷的结果是:

[u'Disks', u'', u'Mem', u'VM Tools', u'Cpu', u'vm-alarm-CPU', u'vm-alarm-MEM', u'vm-alarm-VMTOOLS', u'Uptime']

如你所见,我得到u''为什么?

如果我直接在mongodb中执行此操作,它可以工作:

> db.logs.distinct( "service_description" , { "host_name" : "WEB-01", service_description: {$ne : ""}} )
[
    "Cpu",
    "Disks",
    "Mem",
    "Uptime",
    "VM Tools",
    "vm-alarm-CPU",
    "vm-alarm-MEM",
    "vm-alarm-VMTOOLS"
]

感谢您的帮助

1 个答案:

答案 0 :(得分:3)

您需要将两个find查询字词放入相同的哈希对象中:

hashtable['ACR-WEB-01']=db.logs
    .find({"host_name": "WEB-01", "service_description": {"$ne" : ""}})
    .distinct("service_description")