Home > other >  Upadating documents not possible due to lack of atomic operators?
Upadating documents not possible due to lack of atomic operators?

Time:05-07

I am trying to update my collection through mongodb shell but unsuscessfully due to this error: "Update document requires atomic operators"

db.dogs_main_breed.updateMany([
  {},
  {
    $unwind: "$Behavior"
  },
  {
    $replaceRoot: {
      newRoot: {
        $mergeObjects: [
          "$$ROOT",
          "$Behavior"
        ]
      }
    }
  },
  {
    $project: {
      Behavior: 0
    }
  }
])

How could be possible to update my documents given this query?

My goal is to flatten "Behavior" field and include it in Root directory of my document:

"breed": "Akita",
  "origin": "Japan",
  "url": "https://en.wikipedia.org/wiki/Akita_(dog)",
  "img": "some img url",
  "WikiDescr": [
    {
      "contenido": "Content"
    }
  ],
  "Behavior": [
    {
      "good_with_children": 3,
      "good_with_other_dogs": 1,
    }

Desired output:

"breed": "Akita",
  "origin": "Japan",
  "url": "https://en.wikipedia.org/wiki/Akita_(dog)",
  "img": "some img url",
  "good_with_children": 3,
  "good_with_other_dogs": 1,
  "WikiDescr": [
    {
      "contenido": "Content"
    }
  ],

CodePudding user response:

Can you try out this query?

db.dogs_main_breed.aggregate([
  {
    '$unwind': '$Behavior'
  }, {
    '$replaceRoot': {
      'newRoot': {
        '$mergeObjects': [
          '$$ROOT', '$Behavior'
        ]
      }
    }
  }, {
    '$project': {
      'Behavior': 0
    }
  }, {
    '$out': 'dogs_main_breed'
  }
])

Here the link of Mongo Playground to try out the query

  • Related