如何从pymongo的列表中查询元素 [英] how to query an element from a list in pymongo
本文介绍了如何从pymongo的列表中查询元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
pymongo
在尝试从 tags
db.users.find({"pseudo":"alucaard"}).distinct("produit_up")
Out[1]:
[{u'abus': 0,
u'avctype': u'image/jpeg',
u'date': u'2012-09-15',
u'description': u'le fameux portable solide',
u'id': u'alucaard134766932677',
u'namep': u'nokia 3310',
u'nombre': 1,
u'orientation': u'portrait',
u'photo': ObjectId('5053cd4e3a5f3a0990da8a61'),
u'prix': 1000,
u'tags': [u'solide', u'le', u'fameux', u'portable'],
u'vendu': False}]
list(db.users.find({"solide":{"$in":{"document_up.tags"}}}))
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\IPython\core\interactiveshell.py", line 2746, in run_code exec code_obj in self.user_global_ns, self.user_ns
File "<ipython-input-1-8dff98261d7a>", line 1, in <module>
list(db.users.find({"solide":{"$in":{"document_up.tags"}}}))
File "C:\Python27\lib\site-packages\pymongo\cursor.py", line 778, in next
if len(self.__data) or self._refresh():
File "C:\Python27\lib\site-packages\pymongo\cursor.py", line 729, in _refresh
self.__uuid_subtype))
InvalidDocument: Cannot encode object: set(['document_up.tags'])
注意:这对于 pymongo 用户来说只是一个技巧,如果您的用户的文本大小有限,只需使用集合将其转换,将集合转换为列表:例如:
NB: just a trick for the pymongo users, if your users have a limited size in text, just convert it using a set, the convert the set to a list: for example :
phrase = "hi you, how are you, am i using this"
- 第一步:删除逗号或点以避免正则表达式搜索.
- 其次,使用
phrase.split()
拆分单词. - 将此添加到
set
以避免重复单词. - 将集合转换为
list
- 制作一个包含一些将从列表中删除的单词的字典是个好主意,例如how"、you"、me"……但它会进行大量计算.
- first step: remove comma or dots to avoid regular expression searchs.
- second, use
phrase.split()
to split words. - add this to a
set
to avoid duplicate words. - convert the set to a
list
- it will be a good idea to make a dictionnary that containing some words that will be removed from the list, like "how", "you", "me", ... but it will make a lot of calculation.
希望这个想法会有所帮助.
hope this idea will help.
推荐答案
您的查询有误.尝试更接近的东西:
Your query is wrong. Try something closer to:
list(db.users.find({"document_up.tags":{"$in":["solide"]}}))
这篇关于如何从pymongo的列表中查询元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文