Error: Cannot mix array and object updates

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

I do not think the syntax

is valid inside a expression like it is needed by $cond. More likely something like

{ "$eq" : [ "$status" , "pending" ] ] 

is more appropriate.

I do not know a lot about findOneAndUpdate() but I think it is mongoose specific. May be it (mongoose) is doing some schema validation and prevents you from updating a string field with an JSON object representing an expression.

With the native updateOne() the aggregation version with [ { “$set” : … } ] works perfectly when the appropriate version, mentioned above, of $eq is used.