I am learning a ton when it comes to using MongoDB with Python, but I have one issue I need help on.
I am new to Python and MongoDB and I am developing an e-commerce site for MSMEs to use. I can create the user record using the following query:
mycol.with_options(write_concern=WriteConcern(w = "majority")).insert_one({
"first_name": user.first_name,
"last_name": user.last_name,
"email": user.email,
"hashed_password": user.password,
"phone": 123456789,
"preferences": [],
"status": 'private',
"uid": "None",
"date_created": datetime.datetime.now(),
"date_updated": datetime.datetime.now(),
"is_admin": False,
"user_created": " ",
"user_updated": " ",
# "address": {"physical": {"street": address.street, "street1": address.street1, "city": address.city, "state": address.state, "country": address.country, "postal_code": address.postalCode}},
"to_delete": False,
# 'card':{},
"is_owner": False
})
return user
# return {"success": True}
except Error :
raise Error("A user with the given email address already exists")
I then update the record adding the address object as follows.
try:
#Updates the Physical address object in the user record
updatedUser = mycol.with_options(write_concern=WriteConcern(w="majority")).update_one(
{"email": email},
{"$set": {"address.physical.street": address.street, "address.physical.street1": address.street1, "address.physical.city" : address.city, "address.physical.state": address.state, "address.physical.country": address.country, "address.physical.postal_code": address.postalCode}},
)
#returns user record
return updatedUser
#if there isn't a user with the given email address, raises the belog flag
except Error:
raise Error("A user with the given email address already exists")
Where I am struggling is the fact that in order for me to display the updated record with the address object in the record, I need to query the database again using the find_one query. This seems inefficient. I was trying to assign the result of the query to the variable updatedUser and return the updatedUser to the main program. When I do this I cannot access the address.physical field even though it is successfully updated.
Originally I was just calling the query from main and trying to display it like follows.
user = DB.updateAddress(user[“email”], address)
This should return the updatedUser into the user variable. But when I try to print the user it doesn’t work. The only way I can access the fields is with the following code in Main.
DB.updateAddress(user["email"], address)
updatedUser = DB.getUser(user["email"])
print(updatedUser)
print()
print(updatedUser["address"]["physical"])
Running that code results in this:
{'_id': ObjectId('63835a2336377a211b729a4b'), 'first_name': 'David', 'last_name': 'Thomnpson', 'email': 'dthompson1411@yahoo.com', 'hashed_password': 'RE$etme$200', 'phone': 123456789, 'preferences': [], 'status': 'private', 'uid': 'None', 'date_created': datetime.datetime(2022, 11, 27, 20, 37, 55, 605000), 'date_updated': datetime.datetime(2022, 11, 27, 20, 37, 55, 605000), 'is_admin': False, 'user_created': ' ', 'user_updated': ' ', 'to_delete': False, 'is_owner': False, 'address': {'physical': {'city': 'Singapoe', 'country': 'Singapoe', 'postal_code': 259811, 'state': 'Singapoe', 'street': 'Balmoral Rd2', 'street1': '15-01'}}}
{'city': 'Singapoe', 'country': 'Singapoe', 'postal_code': 259811, 'state': 'Singapoe', 'street': 'Balmoral Rd2', 'street1': '15-01'}
Is there a more efficient way to do this?