简体   繁体   English

Mongodb使用$pull从嵌套的arry中删除对象不能使用pymongo

[英]Mongodb delete object from nested arry using $pull not working using pymongo

   [
    {
      "reports": [
    {
      "imageid": 1888,
      "name": "hello"
    },{
      "imageid": 2344,
      "name": "hello"
    }
      ],
      "report_name": "a"
    },{
    "reports": [
    {
      "imageid": 1888,
      "name": "hello"
    },{
      "imageid": 2344,
      "name": "hello"
    }
      ],
      "report_name": "b"
    }
   ]

Here is my mongodb structure.这是我的 mongodb 结构。 I wants to delete object from reports array where imageid is 1888. Just example in first reports there are 2 objects after delete imageid 1888 there will be onnly one reports (2344 imageid object only).我想从报告数组中删除对象,其中 imageid 为 1888。仅示例在第一个报告中,删除 imageid 1888 后有 2 个对象,将只有一个报告(仅限 2344 imageid 对象)。

db.reports.update(
  { },
  { $pull: { reports: { imageid: 1888 } } },
  { multi: true }
)

This code i am trying in mongodb shell and it is working fine.这段代码我正在 mongodb shell 中尝试,它工作正常。 But below code is using python-pymongo.但下面的代码使用的是 python-pymongo。 That piece of code is not working.那段代码不起作用。

mycol.update(
        {}, 
        { "$pull": { "reports": {"imageid": imageid} } },
        {"multi":"true"}
       )

Here is the errors i am getting while making the query from python using pymongo这是我使用 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

I tried adding upsert=True but still got another error.我尝试添加 upsert=True 但仍然出现另一个错误。

Please have a look请看一看

mycol.update( { }, { "$pull": { "reports": {"imageid": int(imageid)} } }, multi=True )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM