title: Convert ObjectId to String and vice versa inside MongoDB aggregate map
body: <p>I have the following document in a collection:</p>
  
  <p><strong>Input:</strong></p>
  
  <pre><code>{
      "_id" : ObjectId("***"),
      "oldItems" : [ 
          {
              "_id" : ObjectId("***"),
              "name" : "ItemId***",
              "nestedItemsToExtract" : { }
          }
      ]
  }
  </code></pre>
  
  <p>I need to iterate through the <code>oldItems</code> array and create a resultant output document with another array, whose values should be mapped from the original one.</p>
  
  <p><strong>Output:</strong></p>
  
  <pre><code>{
      "_id" : ObjectId("***"),
      "newItems" : [ 
          {
              "oldItemId" : "***", // String Value Of Parent's / Mapped Item Id aka ObjectId.toString()
              "_id" : ObjectId("***") // New ObjectId Here aka ObjectId()
          }
      ]
  }
  </code></pre>
  
  <p>How to convert the mapped (old) <code>_id</code> ObjectId value to a String, but generate a new ObjectId for the (new) <code>_id</code> property in the resultant array?</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>I have managed to find a workaround for generating a new ObjectId value via the <code>"{ $literal: ObjectId() }"</code> expression. I have corrected my "Output" code snippet:</p>
  
  <pre><code>use DB_NAME_HERE
  
  db.getCollection('COLLECTION_NAME_HERE').aggregate([
      { $match: {} },
      { $project: {
          newItems: {
              $map: {
                  input: '$oldItems',
                  as: 'oldItem',
                  in: {
                      oldItemId: '$$oldItem._id' // Returns ObjectId
                      _id: ObjectId() // Fails With 'disallowed field type OID in object expression (at '_id')"'
                      //Edit
                      //_id: { $literal: ObjectId() } // Works
                  }
              }
          }       
      }
  }])
  </code></pre>