How do I query and update a value in an array but the index of that value is unknown and the array is a known key of an object? For example:
doc: {
_id: 1,
stripe: {
// need to update value with ID 2, but do not know the index
accounts: [{name: 'value1' id: 1}, {name: 'value2', id: 2}]
}
}
I'm not sure of the operator/query to do this.
Find doc with _id 1 >> find account with id 2 in doc.stripe.accounts >> update account with id 2
This is what I am doing now, which works, but I know there is a better way. I am querying the doc by _id and then finding the index of the account I want to update and then completely replacing the stripe value.
let obj = doc.stripe.accounts.find(item => {
return item.id === params.externalAccountId;
});
let index = doc.stripe.accounts.indexOf(obj);
let arr = doc.stripe.accounts.slice();
arr[index] = item;
doc.stripe = Object.assign({}, doc.stripe, { accounts: arr });
doc.save((err, doc) => {
callback(null, doc.stripe);
});