OK I played around with it a bit more
and tried 2 workarounds.
WORKAROUND 1 works, but it sucks with complex schemas, because the schema looses it’s protection-functionality… after initially defining it I want to forget about its rules and just use it… when using WORKAROUND 1, I need to think too much about what fields the schema needs for a correct validation (NOTE: it does NOT give us a hint that we are using it correctly)
WORKAROUND 2 is more elegant, but DOES NOT WORK. Maybe there is some kind of library to dynamically build a $set out of an object? Any ideas?
This is the code:
// WORKAROUND 1: (WORKING, BUT SUCKS)
// Let's provide the fields the schema itself needs for full validation
// this means that we need to fully understand the schema-definition
// when using it in a useCase.
//
// This still **sucks**, as we **need to think too much** when using the schema.
var doc = Collection.findOne(id)
Collection.update(id, {$set: {
'accounts.1.password': doc.accounts[1].password, // load the current value from db
'accounts.1.confirmPassword': '9999999999999', // pass in the NEW value
}})
console.log(Collection.findOne().accounts)
/*
// WORKAROUND 2: (NOT WORKING, BUT MORE ELEGANT)
// Let's try to provide SimpleSchema with a full set
// as indicated in Mongos Docs: mongo supports this https://docs.mongodb.org/manual/tutorial/modify-documents/#replace-the-document
var doc = Collection.findOne(id)
doc.accounts[1].confirmPassword = '9999999999999'
// replace the WHOLE doc so that SimpleSchema has all fields
// this throws an ``Error: When the modifier option is true, all validation object keys must be operators. Did you forget `$set`?``
Collection.update(id, doc) // does NOT seem to work in meteor
console.log(Collection.findOne().accounts)
*/
.#2 is indeed the suggested method of updating documents when you are using collection2 or in fact any kind of orm-ish tool. And I think astronomy does the same thing as well.
get a reference to the doc to be updated
set the properties that will change
replace the whole doc
I guess you could rewrite #2 as Collection.update(id, {$set: doc} }