I'm trying to update a nested array in a document only if the array does not already include the item like so:
await userCollection.findOneAndUpdate(
{ _id: ObjectId('616acc597ebda90ca6ffee21') },
{
'devices': {
$push: {
$cond: {
if: {
$in: [req.body.serial, '$devices']
},
then: '$devices',
else: { serial: req.body.serial, data: [] }
}
}
}
},
{ returnOriginal: false },
(err, _) => {
if (err) {
return res.status(400);
} else {
return res.status(200).json(user.value);
}
}
);
This is my user object:
{
"_id": "616acc597ebda90ca6ffee21",
"username": "test@test.com",
"displayName": "Test",
"devices": [
{
"serial": "865674036421275",
"data": [
{
"timestamp": "2021-10-16T12:36:35.799Z"
},
{
"timestamp": "2021-10-16T12:41:33.633Z"
},
{
"timestamp": "2021-10-16T12:52:03.055Z"
}
]
},
],
"hashedPassword": "R5vt3vY7vEvTHvhC7YGNOWuIjBUQGLqsd92QGE06tjU=",
"salt": "6RS0OVIFboCkIEPHdZmTcQ==",
"dateCreated": "2021-10-16T12:58:00.294Z"
}
How will I check if serial is already in devices? Also, is it possible to throw an error if it already exists?