J
J
JRazor2015-05-22 13:54:40
Python
JRazor, 2015-05-22 13:54:40

MongoDB: how to search?

Hello. There was a problem when working with PyMongo - I can’t add to the database through the search, because I can’t properly organize the search. So, there is an array of the form
[{data1: {key1: value1, key2: value2}}, {data2: {key2: value2, key2: value2}}]
We need to add an element to the data_n array, provided that the key and value are sometimes different. Both key and value are in text format.
Question - how to add a new key and a new value to the data_n dictionary, without repetition?
UPD: The array in the latest version looked like this:
[{u'18': {u'hu': u'18 \xe9ves'}, u'_id': ObjectId('555f33062da57a4604e73fc9')}, {u'_id': ObjectId ('555f33062da57a4604e73fca'), u'3d': {u'hu': u'3d'}}] .
Yes, it's a porn site. I'm trying to parse category names in different languages. For a general designation, I store a part of the URL (here it is "18" and "3d"), and I load the language and translation in this language into the key dictionary. It turns out that the main keys will be unchanged, and the key dictionary will expand. And then there was a problem with database.find({}). I don't know what to turn to. How to add new values ​​and key to subdictionary?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
lega, 2015-05-22
@JRazor

Use a dictionary instead of an array:

{
  data1: {key1: value1, key2: value2},
  data2: {key2: value2, key2: value2}
}

db.col.update(match, {$set: {'data2.key2': value2}})
PS: if you brought what you have stored there and what you need to get, it would be better to choose the structure and queries.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question