OJT
OJT

Reputation: 899

Mongo bulk filter and add concatenated field

I have a Mongo collection that I'm trying to update (in PyMongo). I have documents like {'state': 'ny', 'city': 'nyc', 'race': 'b', 'ethnicity': 'h'} and I'm trying to bulk operate on these documents by matching certain criteria and created a concatenated 'race_ethnicity' field.

A pseudo example might be:

filter = {
    'state': 'ny',
    'city': 'nyc',
    'race': {"$exists": True},
    'ethnicity': {"$exists": True},
    'race_ethnicity': {"$exists": False}
}

action = {'$addFields': {'race_ethnicity': {'$concat': ['$race', '$ethnicity']}}}))

Using the above document, the updated document would be: {'state': 'ny', 'city': 'nyc', 'race': 'b', 'ethnicity': 'h', 'race_ethnicity': 'bh'}.

I want to bulk match, and bulk update a collection like this — how do I go about this without getting a BulkWriteError?

*** What I tried:

updates = []
updates.append(UpdateMany({
        'state': 'ny',
        'city': 'nyc',
        "race": {"$exists": True},
        "ethnicity": {"$exists": True},
        "race_ethnicity": {"$exists": False}
    },
    {'$addFields': {'race_ethnicity': {'$concat': ['$race', '$ethnicity']}}}))
self.db.bulk_write(updates) 

This produced the following error:

pymongo.errors.BulkWriteError: batch op errors occurred

Upvotes: 1

Views: 199

Answers (1)

turivishal
turivishal

Reputation: 36144

updates = []

updates.append({
  'updateMany': {
    'filter': {
      'state': 'ny',
      'city': 'nyc',
      'race': { '$exists': True},
      'ethnicity': { '$exists': True},
      'race_ethnicity': { '$exists': False}
    },
    'update': [
      {
        '$set': { 'race_ethnicity': { '$concat': ['$race', '$ethnicity'] } }
      }
    ]
  }
})

self.db.bulk_write(updates)

Playground

Upvotes: 1

Related Questions