mongodbaggregation-frameworkmigrationcompass

MongoDB - bulk updating values in arrays of nested objects


I have a collection in MongoDB that represents the chores kids needs to do each day and whether they are done. Here is an example of a document.

{
  "_id":"2022-02-13:joe",
  "kidName":"Joe",
  "chores": [
    {"chore":"Breakfast: clear dishes and utensils","done":true}, 
    {"chore":"Clearing dishes after meals","done":true},
    {"chore":"Put away coats, knapsack, and shoes","done":true}, 
    {"chore":"Clear toys","done":true}
   ],
    "date": 2022-02-13T00:00:00.000+00:00
  }

I want to change it so that, instead of a Boolean, done is an integer, where:

Meaning value
Done 1
Not Done -1
Unmarked 0

Is there a pipeline or trick in Compass to do this for all documents in the collection?


Solution

  • You could use the update method with aggregation pipeline to update the chores array. You would need to use the following pipeline to achieve the desired results:

    db.collection.updateMany(
        { 'chores.0': { '$exists': true } },
        [
            { '$set': {
                'chores': {
                    '$map': {
                        'input': '$chores',
                        'as': 'chore',
                        'in': {
                            '$mergeObjects': [
                                '$$chore',
                                { 
                                    'done': {
                                        '$switch': {
                                            'branches': [
                                                { 'case': { $eq: [ '$$chore.done', true ] }, then: 1 },
                                                { 'case': { 
                                                    $eq: [ 
                                                        { $ifNull: ['$$chore.done', 'Unmarked'] }, 
                                                        'Unmarked' 
                                                    ] }, 
                                                    then: 0 
                                                },
                                                { 'case': { $eq: [ '$$chore.done', false ] }, then: -1 },
                                            ],
                                            default: 0
                                        }
                                    }
                                }
                            ]
                        }
                    }
                } 
            } }
        ]
    )