【发布时间】:2013-02-11 09:15:57
【问题描述】:
使用 pymongo 我正在尝试检索集合中具有与 null 不同的 SmallUrl 的文档。我正在尝试获取names 密钥和SmallUrl 密钥。
如果我只查找Name,则查询运行良好。但是,由于我想从结果中过滤掉具有 null 值的文档,当我在查询中包含 this 时,查询不会返回任何内容。
这是 MongoDB 结构:
{u'Images': {u'LargeUrl': u'http://somelink.com/images/7960/53.jpg',
u'SmallUrl': u'http://somelink.com/images/7960/41.jpg'}
u'Name': u'Some Name',
u'_id': ObjectId('512b90f157dd5920ee87049e')}
{u'Images': {u'LargeUrl': u'http://somelink.com/images/8001/53.jpg',
u'SmallUrl': null}
u'Name': u'Some Name Variation',
u'_id': ObjectId('512b90f157dd5820ee87781e')}
这是查询的函数:
def search_title(search_title):
$ne
''' Returns a tuple with cursor (results) and the size of the cursor'''
query = {'Name': {'$regex': search_title, '$options': 'i'}, 'SmallUrl': {'$exists': True}}
projection = {'Name': 1, 'Images': 1}
try:
results = movies.find(query, projection)
except:
print "Unexpected error: ", sys.exc_info()[0]
$ne
return results, results.count()
我是 MongoDB 新手,我已经尝试过不同的查询。我用过$and、$not、{'$ne': 'null'}}。我还在 mongoShell 中运行了查询,但结果相同。这是我在 shell 中查询的一个示例:
db.myCollection.find({'Name': {'$regex': 'luis', '$options': 'i'}, 'SmallUrl': {'$ne': null}})
我想知道我做错了什么。
【问题讨论】:
-
在 python 中使用 None 代替 null