【问题标题】:PyMongo: Pull an object in an array of arraysPyMongo:在数组数组中拉出一个对象
【发布时间】:2021-07-01 11:59:53
【问题描述】:

假设我有一个看起来像这样的集合(本质上是一个包含对象的双嵌套数组):

{ 
  'customer': 'bob',
  'products': [
   {
     'id': 5,
     'variants': [
       {'name': 'blue',
       'id': 23},
       {'name': 'orange',
       'id': 13},
       {'name': 'green',
       'id': 53},
      ]    
   }
  ]
},
{ 
  'customer': 'dylan',
  'products': [
   {
     'id': 5,
     'variants': [
       {'name': 'blue',
       'id': 23},
       {'name': 'green',
       'id': 53},
      ]    
   }
  ]
}

我想删除所有variants,其id 位于以下位置:[23, 53] 仅用于bob

{ 
  'customer': 'bob',
  'products': [
   {
     'id': 5,
     'variants': [ 
       {'name': 'orange',
       'id': 13}, 
      ]    
   }
  ]
},
{ 
  'customer': 'dylan',
  'products': [
   {
     'id': 5,
     'variants': [
       {'name': 'blue',
       'id': 23},
       {'name': 'green',
       'id': 53},
      ]    
   }
  ]
}

我有以下内容,但它也删除了dylan 的所有变体:

db.update({'$and': [{'user': 'bob'}, {'products': {'$elemMatch': {'id': 5}}}]}, {'$pull': {'products.$[].variants': {'id': {'$in': [23, 53]}}}}, False, True)

关于如何解决这个问题的任何想法?

【问题讨论】:

标签: python mongodb pymongo pymongo-3.x pymongo-2.x


【解决方案1】:

不清楚您在这里处理的是一条还是两条记录;我假设了两个。不要使用 update() - 它已被弃用 - 使用 update_one()update_many();而您正在查询一个不存在的 user 字段。

鉴于所有这些,请尝试:

db.mycollection.update_one({'customer': 'bob', 'products.variants.id': {'$in': [23, 53]}},
                           {'$pull': {'products.$.variants': {'id': {'$in': [23, 53]}}}})

完整示例:

from pymongo import MongoClient
import json

db = MongoClient()['mydatabase']

db.mycollection.insert_many([
    {
        'customer': 'bob',
        'products': [
            {
                'id': 5,
                'variants': [
                    {'name': 'blue',
                     'id': 23},
                    {'name': 'orange',
                     'id': 13},
                    {'name': 'green',
                     'id': 53},
                ]
            }
        ]
    },
    {
        'customer': 'dylan',
        'products': [
            {
                'id': 5,
                'variants': [
                    {'name': 'blue',
                     'id': 23},
                    {'name': 'green',
                     'id': 53},
                ]
            }
        ]
    }]
)

db.mycollection.update_one({'customer': 'bob', 'products.variants.id': {'$in': [23, 53]}},
                           {'$pull': {'products.$.variants': {'id': {'$in': [23, 53]}}}})

for item in db.mycollection.find({'customer': 'bob'}, {'_id': 0}):
    print(json.dumps(item, indent=4))

打印:

{
    "customer": "bob",
    "products": [
        {
            "id": 5,
            "variants": [
                {
                    "name": "orange",
                    "id": 13
                }
            ]
        }
    ]
}

【讨论】:

    猜你喜欢
    • 2017-08-08
    • 2011-08-12
    • 2019-02-20
    • 2017-06-23
    • 1970-01-01
    • 2021-09-07
    • 1970-01-01
    • 2023-01-25
    • 2023-02-20
    相关资源
    最近更新 更多