I have the following document in my db:
{ "_id": ObjectId("ABCDEFG12345"), "options" : { "foo": "bar", "another": "something" }, "date" : { "created": 1234567890, "updated": 0 } }
And I want to update options.foo and date.updated simultaneously using dot notation, for example:
var mongojs = require('mongojs'); var optionName = 'foo'; var optionValue = 'baz'; var updates = {}; updates['options.' + optionName] = optionValue; updates['date.updated'] = new Date().getTime(); db.myCollection.findAndModify({ query : { _id : ObjectId('ABCDEFG12345') }, update : { $set : updates }, upsert : false, new : true }, function(error, doc, result) { console.log(doc.options); console.log(doc.date); });
And this leads to:
{ foo : 'baz', another : 'something' } { updated : 1234567890 }
In particular, my preexisting date.created field date.created getting knocked down, although I use dot notation.
Why does this only partially work? The options subdocument saves its pre-existing data ( options.another ), why is the existing data saved in the date subdocument?
source share