0

I have the same issue about getting the data updated with pymongo
How to get the data updated with pymongo

But pprint() cannot solve my problem.

Environment: Python, MongoDB, Pymongo

I'm getting "x" as my recent update value, just like getting the "results" in previous link, that why I'm wondering the same solution isn't fit, my x is <pymongo.results.UpdateResult object at 0x00000216021EA080>

print(x)    
print(type(x))

<pymongo.results.UpdateResult object at 0x00000216021EA080>    
<class 'pymongo.results.UpdateResult'>

Here is the code:

import pymongo
import datetime
import json
from pprint import pprint

def init_db(ip, db, coll):
    myclient = pymongo.MongoClient('mongodb://' + ip + '/')
    mydb = myclient[db]
    mycol = mydb[coll]

    return mydb, mycol

def change_db_data(myquery_json, one_or_many_bool, newvalues_json ):
    
    if one_or_many_bool == True:
        x = mycol.products.update_many(myquery_json, newvalues_json)
    else:
        x = mycol.products.update_one(myquery_json, newvalues_json)
    return x

ip_input = input("Enter the ip: ")
exist_DB_name = input("Enter exist DB name: ")
exist_coll_name = input("Enter exist collection name: ")
mydb, mycol  = init_db(ip_input, exist_DB_name, exist_coll_name)

myquery_str = input("Enter ur query: ")
update_one_or_many = input("U are update one or many values? (ex:1 for many , 0 for one): ")
newvalues_str = input("Enter new values: ")

one_or_many_bool = bool(int(update_one_or_many))

myquery_json =json.loads(myquery_str)
newvalues_json =json.loads(newvalues_str)
x = change_db_data(myquery_json, one_or_many_bool, newvalues_json)
print(x)
print(type(x))

pprint(x)
pprint(type(x))

The output

Enter the ip: localhost:27017
Enter exist DB name: (practice_10_14)-0002
Enter exist collection name: collection_new02cp
Enter ur query: { "name": { "$regex": "^R" }, "Age" : { "$gt": 50 } }
U are update one or many values? (ex:1 for many , 0 for one): 1
Enter new values: { "$set": { "name": "Old Mr.R" }}

<pymongo.results.UpdateResult object at 0x00000216021EA080>
<class 'pymongo.results.UpdateResult'>

<pymongo.results.UpdateResult object at 0x00000216021EA080>
<class 'pymongo.results.UpdateResult'>

My print(x) and pprint(x) print the same value, which pprint() isn't work.

I want it print out the modify data

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
j ton
  • 229
  • 9
  • Does this answer your question? [How to get the data updated with pymongo](https://stackoverflow.com/questions/51677814/how-to-get-the-data-updated-with-pymongo) – user20042973 Oct 19 '22 at 02:58
  • @user20042973, I `already` include [same ink](https://stackoverflow.com/questions/51677814/how-to-get-the-data-updated-with-pymongo) in my question, and said their solution not solve my problem yet, therefore `I'm looking for other alternatives for this problem`, that what I meant – j ton Oct 19 '22 at 03:11

1 Answers1

1

But pprint() cannot solve my problem.

The pprint() method is not the solution in the question that you linked. Both that question and the answer use pprint(), so that method is not the problem itself. Rather, the problem in that question is that they were attempting to pprint() the wrong thing. That's the same thing that is happening here.

The answer to that question was to print something other than the object returned directly from the method. Currently, PyMongo returns an UpdateResult object for those update methods. Python doesn't know how to print this object directly:

>>> print(x)
<pymongo.results.UpdateResult object at 0x7fe842511df0>

But you can print its properties:

>>> print(x.modified_count)
1

It also includes a raw_result property which might be what you are interested in:

>>> print(x.raw_result)
{'n': 1, 'nModified': 1, 'ok': 1.0, 'updatedExisting': True}

This all works the exact same with pprint():

>>> pprint(x)
<pymongo.results.UpdateResult object at 0x7fe842511df0>
>>> pprint(x.modified_count)
1
>>> pprint(x.raw_result)
{'n': 1, 'nModified': 1, 'ok': 1.0, 'updatedExisting': True}

Finally, I think you may have copied the code from the other question too directly. Both of your update statements have the following:

... mycol.products.update ...

But products is the name of the collection from the other question. In that other question they are performing db.products.update where they are starting with the database (db) itself. But in your code you have already established the collection object as a variable (mycol = mydb[coll]). And you are attempting to use mycol when doing the update. So your updates should probably drop the reference to products and be something like:

... mycol.update ...
user20042973
  • 4,096
  • 2
  • 3
  • 14