MongoDB query: field either does not exist or has specific value
I'd like to query a mongo collection for records which either don't have a value for a field named 'scheme', or explicitly have the value 'http' for 'scheme'. Sounds pretty easy, but this problem has proved more complex than it first appears.
Since db.collection.find({'开发者_如何学运维scheme': None})
returns all records where 'scheme' is undefined (no index field), I initially assumed the following would work:
db.collection.find({'scheme': {'$in': ['http', None]}})
However, this seems to exclude values in which 'scheme' is undefined, so I can only assume it is searching for records where scheme is either 'http', or explicitly defined to be None
. This seems to be a bit counterintuitive, but there we have it. My second attempt was the following:
db.collection.find( {'$or': [{'scheme': {'$exists': False}}, {'scheme': 'http'}]})
This also excludes result where scheme is undefined. This time, I can't even think of a logical reason why this is failing.
Any ideas why this is failing, and how I can get it to work as desired?
Thanks
EDIT: Just thought I'd note that I'm performing this query through Python (pymongo), which explains the None
(over Javascript's null
)
Resolved: This is apparently an issue of my version of mongodb (1.4.4), the issue is solved in 1.6.5.
精彩评论