[英]PyMongo insert an object into an already existing array in a Document
I want to insert a new object into an array in mongodb everytime a post api request takes place.每次发生 api 请求后,我都想在 mongodb 中的数组中插入一个新的 object。 I am using python and pymongo along with fastapi.我正在使用 python 和 pymongo 以及 fastapi。 Consider this as an example:以此为例:
id1: ObjectId(...)
id2: ObjectId(...)
messages: []
what I want to do is... Everytime an api request takes place a message be added to the messages array.我想要做的是......每次发生 api 请求时,都会将一条消息添加到消息数组中。 Like this:像这样:
id1: ObjectId(...)
id2: ObjectId(...)
messages: [
"Hello"
]
What would be the correct way to do so with pymongo?使用 pymongo 这样做的正确方法是什么?
You may use the update
method with the $push
operator.您可以将update
方法与$push
运算符一起使用。
Maybe the answers for this question will help you.也许这个问题的答案会对你有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.