[pyar] Modificar documento con Pymongo

Emiliano Dalla Verde Marcozzi edvm en fedoraproject.org
Lun Oct 9 17:28:26 ART 2017


El 9 de octubre de 2017, 17:20, Matias Benedetto <matias.benedetto en gmail.com
> escribió:

> Hola a todxs, comento un problema sencillo que estoy teneindo empezando un
> proyecto con Mongo y Python. Lo que quiero hacer es agregarle un campo a un
> documento con un string. Pero no estoy pudiendo. El script no arroja ningún
> error no se agrega el campo "prueba" en los documentos. ¿Se les ocurre cual
> puede ser el problema?
>
> client = MongoClient()
> db = client['DbPruebaPartido']
> collection= db['tweets']
>
> tweets = collection.find({})
>
> for tweet in tweets:
>     db.collection.update({'_id' : tweet["_id"]},
>                      {'$set' : {'prueba' : "probando" }})
>
> Gracias
> Saludos!
> Matias
>
> Buenas Mati, proba con find_one_and_update, te dejo un copy/paste
funcionando:

conversation = conversations.find_one_and_update(
{
'_id': conversation_id,
}, {
'$set': {
'duration_in_seconds': duration_in_seconds,
'confidence': confidence,
}
},
return_document=pymongo.ReturnDocument.AFTER)

Abrazo,
Emi.

pd: Update esta deprecado,
https://api.mongodb.com/python/current/api/pymongo/collection.html?highlight=update#pymongo.collection.Collection.update

-- 
--
iex(1)> [104, 116, 116, 112, 58, 47, 47, 103, 105, 116, 104, 117, 98, 46, 99
, 111, 109,
47, 101, 100, 118, 109]
https://e-dvm.blogspot.com
------------ próxima parte ------------
Se ha borrado un adjunto en formato HTML...
URL: <http://listas.python.org.ar/pipermail/pyar/attachments/20171009/6405c1e2/attachment-0001.html>


Más información sobre la lista de distribución pyar