在 Python 中的字典中的列表中搜索 - Pymongo
Searching in a list inside a dict in Python - Pymongo
希望大家一切都好。
我用 Mongodb 存储了具有以下结构的数据:
{'_id': ObjectId('5f788e29d55317c9cedf2'), 'num': '5512', 'person': [['John', 'Leader'], ['Mary', 'Follower']], 'valid': True}
我正在尝试在关键字“person”中搜索字符串,但到目前为止没有成功。
这是我使用的代码:
name = "John"
rgx = re.compile('.*(%s).*'%name, re.IGNORECASE)
selection = db.collection.find({"person": rgx})
但它 returns 每次都是空的。
关于我做错的原因和地方有什么想法吗?
非常感谢!
我建议您更改数据模型,否则您的查询将是一个痛苦的世界。但如果不能,此查询将使用 $elemMatch
operator:
print(list(db.mycollection.find({'person': {'$elemMatch': {'$elemMatch': {'$in': ['John']}}}})))
希望大家一切都好。
我用 Mongodb 存储了具有以下结构的数据:
{'_id': ObjectId('5f788e29d55317c9cedf2'), 'num': '5512', 'person': [['John', 'Leader'], ['Mary', 'Follower']], 'valid': True}
我正在尝试在关键字“person”中搜索字符串,但到目前为止没有成功。
这是我使用的代码:
name = "John"
rgx = re.compile('.*(%s).*'%name, re.IGNORECASE)
selection = db.collection.find({"person": rgx})
但它 returns 每次都是空的。
关于我做错的原因和地方有什么想法吗?
非常感谢!
我建议您更改数据模型,否则您的查询将是一个痛苦的世界。但如果不能,此查询将使用 $elemMatch
operator:
print(list(db.mycollection.find({'person': {'$elemMatch': {'$elemMatch': {'$in': ['John']}}}})))