1

Im using pymongo version 4.3.3 and motor version 3.1.2

This is my code:

async def add_reputation_to_db(
    self,
    guild_id: hikari.Snowflake,
    user_recieve_id: hikari.Snowflake,
) -> None:
    await self._guild_collection.update_one({'_id': guild_id}, {'$inc': {f'{user_recieve_id}.reputation': 1}})

I have no errors with it, but the problem is that database does not update any data if I use a point to jump to a certain key in the dictionary, in my case it is f'{user_recieve_id}.reputation'.

The hierarchy of my document looks like this:

{
    '_id': guild_id,
    'user_id': {
        'reputation': 123
    }        
}

But user_id must be the user identifier as number.

I looked for solutions on the Internet and ChatGPT, but it did not give me any results.

Saeed
  • 3,255
  • 4
  • 17
  • 36
minikslyh
  • 11
  • 2

1 Answers1

1

This code snippet seems to work. Note that if you don't have a matching _id nothing will be set.

from pymongo import MongoClient

db = MongoClient()['mydatabase']
guild_id = 1
user_recieve_id = 'user1'
coll.insert_one({"_id": guild_id})

coll.update_one({'_id': guild_id}, {'$inc': {f'{user_recieve_id}.reputation': 1}})
print(coll.find_one())

coll.update_one({'_id': guild_id}, {'$inc': {f'{user_recieve_id}.reputation': 1}})
print(coll.find_one())

prints:

{'_id': 1, 'user1': {'reputation': 1}}
{'_id': 1, 'user1': {'reputation': 2}}
Belly Buster
  • 8,224
  • 2
  • 7
  • 20