0

Here is my code :

results = db.products.update_many({"brandReference": idMongoToReplace}, {'$set': {'brandReference':idMongoToUse}},ReturnDocument.AFTER)
pprint(results)

I would like to have an array with the updated objects. (the operations is ok in mongo, but i want to get back the object updated)

For the moment i have a :

<pymongo.results.UpdateResult object at 0x7f9d913ad208>
Bussiere
  • 500
  • 13
  • 60
  • 119
  • Looks kinda like sqlite in REPL. It's returning a cursor object to you. Have you checked to see if the database is updated? – roganjosh Aug 03 '18 at 17:59
  • @roganjosh the database is updated but i want the objects updated – Bussiere Aug 03 '18 at 18:46
  • I don't know what that means, sorry. – roganjosh Aug 03 '18 at 18:47
  • If you're asking to see what records are updated, you have that info before you even execute your statement/query? The only question is whether your changes to the DB work or not. I'm not sure what else you want to see. – roganjosh Aug 03 '18 at 18:52

1 Answers1

2

you extract results from the cursor, like this:

results = db.products.update_many({"brandReference": idMongoToReplace}, {'$set': {'brandReference':idMongoToUse}},ReturnDocument.AFTER)
for doc in results: 
    pprint(doc)

or turn the cursor into a list like this:

results = list(db.products.update_many({"brandReference": idMongoToReplace}, {'$set': {'brandReference':idMongoToUse}},ReturnDocument.AFTER))
pprint(results)

How can I return an array of mongodb objects in pymongo (without a cursor)? Can MapReduce do this?

Paolo
  • 704
  • 9
  • 24