bit of a strange query.
I have the below. collection accounts and i have script which will run and needs to do the following:
- if fileNumber exists AND balance array([{ value, date}]) already has the item[11] value dont update
- if fileNumber exists and balance array DOESNT have the value - update the balance array
- if fileNumber doesnt exists upsert one
Account.findOneAndUpdate(
{ fileNumber: item[0], 'balance.value': { $ne: item[11] } },
{
$set: {
clientCode: item[1],
clientRef: item[2],
name: item[3],
phoneNumber1: item[4],
phoneNumber2: item[5],
phoneNumber3: item[6],
address1: item[7],
postcode: item[8],
exec: item[9],
dateOfBirth: item[10],
instalmentAmount: item[12],
},
$addToSet: { balance: { value: item[11], date: Date.now() } },
},
{ upsert: true },
);
but at the moment it only wors half way. It will upsert if fileNumber exists but value in balance array doesnt, which i dont want
Could anyone help please?
Thanks