$eq inside findoneAndUpdate not working

const classRes = await privateClass.findOneAndUpdate({classId:req.query.classId,teacherId:user.id},
            {$set:{classStarted:{ $eq: [false, "$classStarted"] }}},{new:true})

classStarted is of Boolean type, and I want change it to opposite of current value.
But this doesn’t work.

Hi,

You can do it with Aggregation Framework. In Aggregation Framework, you can access the current values of properties and based on that calculate new values. Also, there is $not operator that you can use. You can do it like this:

db.collection.update({
  "_id": 1
},
[
  {
    "$set": {
      "classStarted": {
        "$not": "$classStarted"
      }
    }
  }
],
{
  "new": true
})

Working example

its showing error

CastError: Cast to Boolean failed for value "{ '$not': '$classStarted' }" (type Object) at path "classStarted"
    at model.Query.exec (/Users/abhijeetarya/web development/tessers2/node_modules/mongoose/lib/query.js:4650:21)
    at model.Query.Query.then (/Users/abhijeetarya/web development/tessers2/node_modules/mongoose/lib/query.js:4749:15)
    at processTicksAndRejections (node:internal/process/task_queues:96:5) {
  messageFormat: undefined,
  stringValue: `"{ '$not': '$classStarted' }"`,
  kind: 'Boolean',
  value: { '$not': '$classStarted' },
  path: 'classStarted',
  reason: CastError: Cast to boolean failed for value "{ '$not': '$classStarted' }" (type Object) at path "undefined"
      at castBoolean (/Users/abhijeetarya/web development/tessers2/node_modules/mongoose/lib/cast/boolean.js:28:9)
      at SchemaBoolean.cast (/Users/abhijeetarya/web development/tessers2/node_modules/mongoose/lib/schema/boolean.js:207:12)
      at SchemaBoolean.SchemaType.applySetters (/Users/abhijeetarya/web development/tessers2/node_modules/mongoose/lib/schematype.js:1179:12)
      at SchemaBoolean.SchemaType._castForQuery (/Users/abhijeetarya/web development/tessers2/node_modules/mongoose/lib/schematype.js:1613:15)
      at SchemaBoolean.castForQuery (/Users/abhijeetarya/web development/tessers2/node_modules/mongoose/lib/schema/boolean.js:236:15)
      at SchemaBoolean.SchemaType.castForQueryWrapper (/Users/abhijeetarya/web development/tessers2/node_modules/mongoose/lib/schematype.js:1580:20)
      at castUpdateVal (/Users/abhijeetarya/web development/tessers2/node_modules/mongoose/lib/helpers/query/castUpdate.js:527:19)
      at walkUpdatePath (/Users/abhijeetarya/web development/tessers2/node_modules/mongoose/lib/helpers/query/castUpdate.js:260:22)
      at castUpdate (/Users/abhijeetarya/web development/tessers2/node_modules/mongoose/lib/helpers/query/castUpdate.js:96:7)
      at model.Query._castUpdate (/Users/abhijeetarya/web development/tessers2/node_modules/mongoose/lib/query.js:4859:10) {
    stringValue: `"{ '$not': '$classStarted' }"`,
    messageFormat: undefined,
    kind: 'boolean',
    value: { '$not': '$classStarted' },
    path: undefined,
    reason: undefined,
    valueType: 'Object'
  },
  valueType: 'Object'
}

Hi,

Can you copy the whole query here? Note that update argument needs to be wrapped with an array syntax []. So, $set should be inside the array.

I am not sure Abhijeet_Arya could do that. The error is inside the mongoose obstruction layer which do some validation, to help the user, before sending the command to the server.

I do not know if mongoose allows the new 4.2 update with aggregation. Mongoose experts please help.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.