I just encountered an issue that I think turned out to be two different issues.
using this (partial) Schema
var MrQuotes = this.MR.model('Mrquotes', {
quote_number: Number,
quote_version: Number,
broker_id: { type: Schema.Types.ObjectId, ref: 'Brokers' },
mrClient_ids: [{type: Schema.Types.ObjectId, ref: 'Mrclients' }],
stat: String,
create_date: Date,....
I am able to retrieve records with all manner of queries against the model client side. No problems, in fact it works incredibly well. When I add a new object to the mrClient_ids array and call update that also works. However I found that if I sliced an item out of the array and called update the previous state was serialized to the database without the desired change. I tracked this down to mr-rpc-methods and compared toUpdate with doc after the _.merge(doc, toUpdate) and found that underscore was not merging the changes. I noticed that since I had been using populate that the objects looked very different and I guessed that lodash was probably getting confused. As you know the original document consists of the array of object ID's and not the fully populated document. So I tried an experiment. Before calling update I did a foreach on the collection, extracted just the _id's into an array, assigned them to the model and then ran the update. I actually expected this to work but it also didn't. I still don't know why. So I thought about replacing lodash for this, just to simplify things and used plain JS as described here: https://plainjs.com/javascript/utilities/merge-two-javascript-objects-19/
Using that technique I was then able to update both adding and removing objects. In either case I have retained the approach of updating the model with only the id's as that seems to be a cleaner solution rather than hoping that everything in between handles the nested objects correctly.
Any thoughts on this?
Thanks and by the way great work on this project. It's incredibly useful.
/W