假设我有这个文档结构:
{
"_id": <ObjectId goes here>,
"postBody": "test",
"photos": [
{"url": "url1.com", "status": 0},
{"url": "url2.com", "status": 0},
{"url": "url3.com", "status": 0},
{"url": "url4.com", "status": 0}
]
}
我有这个要删除的网址列表
toDelete = ["url2.com", "url3.com"]
如何在 mongo 查询中执行此操作?
这是我到目前为止尝试过的
db.Post.update_one(
{"_id": <ObjectId goes here>},
{
"$pull": {
"photos": {
"url": {
"$in": toDelete
}
}
}
}
)
但不知何故,它不起作用。我该怎么做?
更新后的文档应该看起来像这样
{
"_id": <ObjectId goes here>,
"postBody": "test",
"photos": [
{"url": "url1.com", "status": 0},
{"url": "url4.com", "status": 0}
]
}
我需要帮助。
更新:我收到此错误
Traceback (most recent call last):
File "/var/task/chalice/app.py", line 1104, in _get_view_function_response
response = view_function(**function_args)
File "/var/task/chalicelib/Post/controllers/post_controller.py", line 62, in edit_post
raise e
File "/var/task/chalicelib/Post/controllers/post_controller.py", line 59, in edit_post
result = POSTSERVICE.edit_post(post_data)
File "/var/task/chalicelib/Post/services/post_service.py", line 362, in edit_post
raise e
File "/var/task/chalicelib/Post/services/post_service.py", line 315, in edit_post
"$in": toDelete
File "/var/task/pymongo/collection.py", line 1003, in update_one
session=session),
File "/var/task/pymongo/collection.py", line 856, in _update_retryable
_update, session)
File "/var/task/pymongo/mongo_client.py", line 1492, in _retryable_write
return self._retry_with_session(retryable, func, s, None)
File "/var/task/pymongo/mongo_client.py", line 1385, in _retry_with_session
return func(session, sock_info, retryable)
File "/var/task/pymongo/collection.py", line 852, in _update
retryable_write=retryable_write)
File "/var/task/pymongo/collection.py", line 767, in _update
common.validate_boolean("upsert", upsert)
File "/var/task/pymongo/common.py", line 159, in validate_boolean
raise TypeError("%s must be True or False" % (option,))
TypeError: upsert must be True or False