A
A
AlmazKayum2018-04-12 02:19:08
Python
AlmazKayum, 2018-04-12 02:19:08

How to update MongoDB document values ​​using PyMongo?

Good time!
Let's say we added a document to the collection:

db.goalsdoc.insert_one({'chat_id': message.chat.id, 'goals': [{'theme': message.text, 'date': '', 'subgoals': []}]})

We want to update the date key value and add an element to the subgoals list with message.text objects. According to the MongoDB documentation, this is done like this:
db.goalsdoc.update_one({'chat_id': message.chat.id}, {'$set': {'goals.date': message.text}})
db.goalsdoc.update_one({'chat_id': message.chat.id}, {'$push': {'goals.subgoals': message.text}})

But after compilation, the following error occurs:
cannot use the part to traverse the element python
Please tell me what I'm doing wrong? And how to do it right?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
pcdesign, 2018-04-12
@pcdesign

db.goalsdoc.update ({'chat_id': message.chat.id}}, { '$set': {"goals.0.date" : message.text} })

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question