[CSHARP-2613] Update an entity field with data from another entity field Created: 21/May/19  Updated: 04/Jan/22  Resolved: 25/Jun/20

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

Type: New Feature Priority: Major - P3
Reporter: Rick van Lieshout Assignee: Wan Bachtiar
Resolution: Done Votes: 0
Labels: rfw
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Any



 Description   

In short. I want to be able to issue a `.Set` and reference the existing document(/entity) for fields.

I posted a question on [StackOverflow|https://stackoverflow.com/questions/56217554/mongodb-c-sharp-driver-update-an-entity-field-with-data-from-another-entity-fie] which goes like this:
My model looks something like this:
 data: {
field1 :"1"
},

history: [{
field1 :"0"
}]
{{}}

Now I want to create an update definition which takes the current object in "data" and inserts it into the "history" array.

I know how to do this with absolute values..:
 var updateDef = Builders<Entity>.Update.Push(x => x.History, historyObject);
updateDef = updateDef.Set(x => x.Data, newDataModel);
The problem is that I want the "historyObject" to be automatically gotten from the current document's "data" field.



 Comments   
Comment by Rick van Lieshout [ 25/Jun/20 ]

You transitioned it to "Community answered", but what is the answer ?

Comment by Wan Bachtiar [ 29/Oct/19 ]

The problem is that I want the "historyObject" to be automatically gotten from the current document's "data" field.

You can do this with Update Using Aggregation Pipeline feature in MongoDB v4.2+. This allows you to use the expressive aggregation pipeline to update documents in the collection without knowing the value of `data`. For example, to automatically fetch the data field and pushed into history field :

            var pipeline = new BsonDocumentStagePipelineDefinition<BsonDocument, BsonDocument>(
                            new[] { 
                                new BsonDocument{{"$set", 
                                        new BsonDocument{{"history", 
                                            new BsonDocument{{ "$concatArrays", 
                                                new BsonArray{ "$history", new BsonArray{ "$data" } }  }} 
                                        }}
                                }} }
            );
            var updateDefinition = new PipelineUpdateDefinition<BsonDocument>(pipeline);
 
            var result = collection.UpdateOne(new BsonDocument{}, updateDefinition);

Note that PipelineUpdateDefinition is available from MongoDB .NET/C# driver v2.9+.

Please note that the CSHARP project is for reporting bugs or feature suggestions for the MongoDB .NET/C# driver. If you have follow-up questions on the use of the C# driver, please post a question on the MongoDB Community forums dot-net with the relevant information.

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