I'm trying to determine if the following use case is feasible in MongoDB 3.4: I have a collection containing documents with a schema similar to this:
{
"_id": ObjectId("..."),
"data": {
"fname": "John",
"lname": "Doe"
},
"history": []
}
The data
field is meant to hold the current state of the entity and the history
array is meant to hold the most recent versions of it (accounting for the max document size and for querying purposes).
When an update operation is issued from application logic, I would like to $push
the data
field into the history
array and the replace it with the new state of the entity. I haven't been able to find anything about this on the MongoDB docs and every question I found on SO
deals with pushing a totally new object into an array, not "moving" an existing field's value. Is it possible to reference a document field in a $push
operation like this?
db.collection.update({"_id": "1234"}, {"$set": {"data": {"data-value"}}}, {"$push": {"history": {"history-value"}})
你可以这样写,它会自动将值推送到历史记录中,并将数据更改为当前状态。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.