Hi there! I am trying to update a a field base on its actual condition which seems to be pretty simple using db.findOneAndUpdate
and the $cond
method. But I am getting this error no matter what I try:
Cast to string failed for value "{ '$cond': [ { '$…s', '$status' ] }" (type Object) at path "status"
My query is the following:
await db.findOneAndUpdate(
{ projectId: new ObjectId(projectId) },
{
$set: {
status: {
$cond: [{ $eq: 'pending' }, 'in_progress', '$status'],
},
},
},
{ new: true, upsert: true }
);
I believe that error comes from not using the aggregation pipeline. So I tried to use it like this adding [ … ]:
await db.findOneAndUpdate(
{ projectId: new ObjectId(projectId) },
[{
$set: {
status: {
$cond: [{ $eq: 'pending' }, 'in_progress', '$status'],
},
},
}],
{ new: true, upsert: true }
);
But then I get this error:
Cannot mix array and object updates
Could someone tell me what am I doing wrong? Thank You!
I am using Mongoose: ^6.2.3