【发布时间】:2020-03-27 20:36:48
【问题描述】:
[
{
"reports": [
{
"imageid": 1888,
"name": "hello"
},{
"imageid": 2344,
"name": "hello"
}
],
"report_name": "a"
},{
"reports": [
{
"imageid": 1888,
"name": "hello"
},{
"imageid": 2344,
"name": "hello"
}
],
"report_name": "b"
}
]
这是我的 mongodb 结构。我想从报告数组中删除对象,其中 imageid 为 1888。例如,在第一个报告中,删除 imageid 1888 后有 2 个对象,将只有一个报告(仅限 2344 imageid 对象)。
db.reports.update(
{ },
{ $pull: { reports: { imageid: 1888 } } },
{ multi: true }
)
我正在 mongodb shell 中尝试此代码,它工作正常。 但下面的代码使用的是 python-pymongo。那段代码不起作用。
mycol.update(
{},
{ "$pull": { "reports": {"imageid": imageid} } },
{"multi":"true"}
)
这是我在使用 pymongo 从 python 进行查询时遇到的错误
File "/home/fractaluser/Desktop/Dev/consumerhubfractal_rb/env/lib/python3.5/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/fractaluser/Desktop/fractal/consumerhubfractal_rb/report/views.py", line 45, in report
{"multi":"true"}
File "/home/fractaluser/Desktop/Dev/consumerhubfractal_rb/env/lib/python3.5/site-packages/pymongo/collection.py", line 2535, in update
collation=collation)
File "/home/fractaluser/Desktop/Dev/consumerhubfractal_rb/env/lib/python3.5/site-packages/pymongo/collection.py", line 732, in _update
common.validate_boolean("upsert", upsert)
File "/home/fractaluser/Desktop/Dev/consumerhubfractal_rb/env/lib/python3.5/site-packages/pymongo/common.py", line 132, in validate_boolean
raise TypeError("%s must be True or False" % (option,))
TypeError: upsert must be True or False
我尝试添加 upsert=True 但仍然出现另一个错误。
请看一下
【问题讨论】: