title: MongoDB - change simple field into an object
body: <p>In MongoDB, I want to change the structure of my documents from:</p>
  
  <pre><code>{
      discount: 10,
      discountType: "AMOUNT"
  }
  </code></pre>
  
  <p>to:</p>
  
  <pre><code>{
      discount: {
          value: 10,
          type: "AMOUNT"
      }
  }
  </code></pre>
  
  <p>so I tried following query in mongo shell:</p>
  
  <pre><code>db.discounts.update({},
      {
          $rename: {
               discount: "discount.value",
               discountType: "discount.type"
          }
      },
      {multi: true}
  )
  </code></pre>
  
  <p>but it throws an error:</p>
  
  <pre><code>"writeError" : {
      "code" : 2,
      "errmsg" : "The source and target field for $rename must not be on the same path: discount: \"discount.value\""
  }
  </code></pre>
  
  <p>A workaround that comes to my mind is to do it in 2 steps: first assign the new structure to a new field (let's say <code>discount2</code>) and then rename it to <code>discount</code>. But maybe there is a way to do it one step?</p>