I want to save changes made to my document. The easiest way to do this is to store the actual changes made to a document. What I mean:
var changes = {
$set: {
text: 'Some text.'
}
}
db.posts.update({
_id: _id
}, changes)
db.changes.insert({
postid: _id,
changes: changes
})
However I’m getting the error (with good reason):
Error: key $set must not start with '$'
What’s the easiest way to store changes?
Or perhaps I’m approaching the problem wrong and you have a better solution. I want users to be able to see a log of changes people make to any post or, in fact, anything. I’m not going to make a function for every time of change. Editing the text is just one of many ways to make changes to a document.
This is a limitation within MongoDB. There are certain reserved characters one of them being
$due to how querying must work. When using operators there would be ambiguity between the document in the collection and the document used for updating.I would recommend stripping out the
$symbols. I would instead use these words in place of the operators you are trying to use: