Home > Mobile >  Migrationscript with for MongoDB Collection
Migrationscript with for MongoDB Collection

Time:09-21

i need help for migrationscript for a mongodb collection.

This is a example how it looks like and I wanna update it that the Error field should be an array in the future.

{
    "_id": {
        "$oid": "632a11c91f786f7d86d7633a"
    },
    "List": [
      {
        "Name": "WithError",
        "Error": "duplicate"
      },
      {
        "Name": "WithOutError"
      }
    ]
}

This is a example how it should look like:

{
  "_id": {
      "$oid": "632a11c91f786f7d86d7633a"
  },
  "List": [
    {
      "Name": "WithError",
      "Error": ["duplicate"]
    },
    {
      "Name": "WithOutError"
    }
  ]
}

My plan is to use updateMany, but I need help for the filter and how to do the update.

Kind Regards and Thx for the help

CodePudding user response:

tried so i wanna have the following changes:

  • all documents in the collection should be updated
  • to check if we wanna update I just want to use the $exists expression

What do I need to change, that the $exists expression is working ?

// Select the database to use.
use('Oddb');
db.collection.updateMany({},
[
  {
    "$set": {
      "List": {
        $map: {
          input: "$List",
          as: "element",
          in: {
            $cond: {
              if: {
                $exists: [
                  "Error",
                  true
                ]
              },
              then: {
                Name: "$$element.Name",
                Error: [
                  "$$element.Error"
                ]
              },
              else: "$$element"
            }
          }
        }
      }
    }
  }
])

CodePudding user response:

You can use aggregation pipeline within updateMany, something like this:

db.collection.update({},
[
  {
    "$set": {
      "List": {
        $map: {
          input: "$List",
          as: "element",
          in: {
            $cond: {
              if: {
                $ifNull: [
                  "$$element.Error",
                  false
                ]
              },
              then: {
                Name: "$$element.Name",
                Error: [
                  "$$element.Error"
                ]
              },
              else: "$$element"
            }
          }
        }
      }
    }
  }
])

Here's the playground link. With updateMany the query will be:

db.collection.updateMany({},
[
  {
    "$set": {
      "List": {
        $map: {
          input: "$List",
          as: "element",
          in: {
            $cond: {
              if: {
                $ifNull: [
                  "$$element.Error",
                  false
                ]
              },
              then: {
                Name: "$$element.Name",
                Error: [
                  "$$element.Error"
                ]
              },
              else: "$$element"
            }
          }
        }
      }
    }
  }
])

CodePudding user response:

But to call this update often I need to check if Error is off type string, how can I do this?

I tried this, but it's not working

// Select the database to use.
use('Oddb');
db.KeyFigureCollection.updateMany({
},
[
  {
    "$set": {
      "KeyFigures": {
        $map: {
          input: "$KeyFigures",
          as: "element",
          in: {
            $cond: {
              if: {
                $ifNull: [
                  "$$element.Error",
                  false
                ],
                $eq: [$type("$$element.Error"), "string"]
              },
              then: {
                $mergeObjects: [
              "$$element",
              {
                Error: [
                  "$$element.Error"
                ]
              }]
                
              },
              else: "$$element"
            }
          }
        }
      }
    }
  }
])

  • Related