简体   繁体   English

PyMongo:在 arrays 数组中拉取一个 object

[英]PyMongo: Pull an object in an array of arrays

Let's say I have a collection that looks like this (essentially a double-nested array holding objects):假设我有一个看起来像这样的集合(本质上是一个包含对象的双嵌套数组):

{ 
  '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},
      ]    
   }
  ]
}

I would like to remove all variants whose id is in the following: [23, 53] for only bob我想删除id如下的所有variants[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},
      ]    
   }
  ]
}

I have the following, however it also removes all variants for dylan :我有以下内容,但是它也删除了dylan的所有变体:

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

Any ideas on how to approach this?关于如何解决这个问题的任何想法?

It's not clear if you are dealing with one or two records here;不清楚您在这里处理的是一两条记录; I've assumed two.我假设了两个。 Don't use update() - it's deprected - use update_one() or update_many() ;不要使用update() - 它已被弃用 - 使用update_one()update_many() and you're querying on a user field that doesn't exist.并且您正在查询一个不存在的user字段。

Given all that, try:鉴于这一切,请尝试:

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

Full example:完整示例:

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))

prints:印刷:

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

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

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