mongodb - pushing object to array attribute when updating a document -
i'm using mongoose schema & persistence layer.
when adding item array-attribute in existing document, fail: err equal null
, raw equal { n: 0, nmodified: 0, ok: 1 }
.
any idea why happens?
update code
let concreteanswer = { questionid: 1, answer: { rating: 34 } }; participation.update( { _id: 'some-unique-id', 'answers.$.instrumentname': 'some-instrument-name' // instrumentname unique within answers array }, { $push: {"answers.$.concreteanswers" : concreteanswer} }, (err, raw) => { console.log(`error occured! raw: ${raw}, error: ${err}`); } );
schema definition
var participationschema = new schema({ answers: [{ instrumentname: { type: string, enum: validinstruments }, concreteanswers: [{ questionid: number, answer: {} }] }] // further attributes omitted readability }, { timestamps: true }); var participation = mongoose.model('participation', participationschema);
document in db
{ "_id": "some-unique-id", "answers": [ { "instrumentname": "some-instrument-name", "_id": "another-unique-id", "concreteanswers": [] }, // etc. }
2017-04-16: changed update code according @veeram's comments:
participation.update( { _id: mongoose.types.objectid('some-unique-id'), 'answers.instrumentname': 'some-instrument-name' }, { $push: {"answers.$.concreteanswers" : concreteanswerdto} }, // err handling above }
i realized, operation works (i.e. doc updated in db), still execute console.log
within error handling.
Comments
Post a Comment