0

I'm using the following packages:

  • MongoDB.Driver - Version="2.7.2"
  • MongoDB.Driver.Core - Version="2.7.2"

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.

Any ideas?

Current workaround

To work around this problem I'm currently fetching the document first and using the data field from the result.

The problem with this is that if the data changes in the meantime we'll overwrite it with the .Set operation without pushing it to the history array.

Rick van Lieshout
  • 2,276
  • 2
  • 22
  • 39
  • Unfortunately there's no such operation in MongoDB update API. Your workaround is the best thing you can do. If you feel that such operator should be added you can open a ticket here: https://jira.mongodb.org/secure/Dashboard.jspa – mickl May 20 '19 at 16:18
  • Hey @mickl, That's what I thought but I was afraid I missed something... Thanks. – Rick van Lieshout May 21 '19 at 08:12

0 Answers0