[CSHARP-2516] Update field in document with value of other field in document Created: 12/Feb/19  Updated: 27/Oct/23  Resolved: 19/Mar/19

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: New Feature Priority: Minor - P4
Reporter: KevinBrok Assignee: Unassigned
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 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);

 

 



 Comments   
Comment by Wan Bachtiar [ 06/Mar/19 ]

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.

Hi Kevin,

This request is more relevant for the MongoDB server instead of MongoDB .NET/C# driver itself. What you've mentioned have been requested in SERVER-11345. Please upvote/watch the ticket to receive notifications on it.

As a temporary migration workaround, you could utilise $out aggregation stage to output a copy of the updated collection. For example:

    var pipeline = new BsonDocument[]{
        new BsonDocument{ {"$project", new BsonDocument{{"ClassificationId", "$Classification._id"}, {"Foo", 1} }} }, 
        new BsonDocument{ {"$out", "collection_copy"}}
    };
    var aggregate = collection.Aggregate<BsonDocument>(pipeline);

Or, alternatively iterating through the whole collection to capture the document's value. Although this means reading all documents to the client. For example:

    collection.Find(new BsonDocument()).ForEachAsync(doc => {
        collection.UpdateOne(
            new BsonDocument{{"_id", doc.GetValue("_id")}}, 
                new BsonDocument{
                    {"$set", new BsonDocument{{"ClassificationId", doc["Classification"].AsBsonDocument.GetValue("_id")}}},
                    {"$unset", new BsonDocument{{"Classification", 1}}}
                });
        }
    );

If you have further questions relating on the use of MongoDB .NET/C# driver please start a new discussion on mongodb-user group with relevant information.

Regards,
Wan.

Comment by KevinBrok [ 12/Feb/19 ]

Wish I could fix the spell errors but turns out, I can't change the description.

Generated at Wed Feb 07 21:42:46 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.