Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-2516

Update field in document with value of other field in document

    XMLWordPrintableJSON

Details

    • Icon: New Feature New Feature
    • Resolution: Works as Designed
    • Icon: Minor - P4 Minor - P4
    • None
    • None
    • None
    • None

    Description

      We are working on an application which uses a MongoDB. Since the application is being used in Production we have to create Datamigration steps so the database cna be used in the newer version. A couple of times we moved a subdocument ot its own collection or we had to update the value of some documents to a different value which is also present in the document.

      What we do now is to get the document form the database. The set the value of the document with the new value, or we create an update command, and save it back to the database.

      What we would like to have is a way to update the value of a field with the value of another field in the same document.

      For example we have the following document:

       

      {
      	_id: some_id,
      	Classification: {
      		_id: some_other_id,
      		Type: 0
      	}
      }

      We need to save the ClassificationId in the document instead of the whole Classification like this:

       

      {
      	_id: some_id,
      	ClassificationId: some_other_id
      }

       

      Maybe this can be done with something like this:

       

      var collection = data.Database.GetCollection<Person>("Person");
      var update = Builders<Person>.Update
          .SetInDocument("ClassificationId", "Classification._id")
          .Unset("Classification");
      var result = await collection.UpdateManyAsync(Builders<Person>.Filter.Empty, update);
      

       

       

      Attachments

        Activity

          People

            Unassigned Unassigned
            kevinbrok KevinBrok
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: