pymongo
throws me an error when trying to query and element from 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'])
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()
to split words.set
to avoid duplicate words.list
hope this idea will help.
Your query is wrong. Try something closer to:
list(db.users.find({"document_up.tags":{"$in":["solide"]}}))
9 years later, I stumbled across this page when seeking a similar answer. Since the only other answer appears suspicious, despite the votes, I'm adding this one as a resource for other visitors.
This reference seems to be appropriate: https://docs.mongodb.com/manual/tutorial/query-arrays/#query-an-array-for-an-element
To query if the array field contains at least one element with the specified value, use the filter
{ <field>: <value> }
where<value>
is the element value.The following example queries for all documents where tags is an array that contains the string "red" as one of its elements:
cursor = db.inventory.find({"tags": "red"})
Explicitly, you could use list(db.users.find({"document_up.tags":"solide"}))
currently, as attempting to match a single value against an array implicitly returns records with arrays containing (at least one instance of) that value. The reference goes into pretty useful detail on specific cases.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With