Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

362
Views
Getting error only in mongoose, not in mongo shell Error: Can't use $or

I have a blog collection whose schema is something like below

{
 content:String,
 type:String,
 author:Number
}

Query

    const edit = await Posts.findByIdAndUpdate(
      {
        _id: postId,
        $or: [
          { type: "blog" },
          { author: 123 },
        ],
      },
      {
        $set: {
          content:"...new"
        },
      }
    );

On executing this I am getting the below error

Error: Can't use $or
    at ObjectId.SchemaType.castForQuery (/home/rahul/Desktop/projects/components/components-server/node_modules/mongoose/lib/schematype.js:1539:13)
    at ObjectId.SchemaType.castForQueryWrapper (/home/rahul/Desktop/projects/components/components-server/node_modules/mongoose/lib/schematype.js:1511:22)
    at cast (/home/rahul/Desktop/projects/components/components-server/node_modules/mongoose/lib/cast.js:316:39)
    at model.Query.Query.cast (/home/rahul/Desktop/projects/components/components-server/node_modules/mongoose/lib/query.js:4963:12)
    at castQuery (/home/rahul/Desktop/projects/components/components-server/node_modules/mongoose/lib/query.js:4764:18)
    at model.Query.Query._findAndModify (/home/rahul/Desktop/projects/components/components-server/node_modules/mongoose/lib/query.js:3707:23)
    at model.Query.<anonymous> (/home/rahul/Desktop/projects/components/components-server/node_modules/mongoose/lib/query.js:3267:8)
    at model.Query._wrappedThunk [as _findOneAndUpdate] (/home/rahul/Desktop/projects/components/components-server/node_modules/mongoose/lib/helpers/query/wrapThunk.js:27:8)

But the same query is working with CLI mongo shell

db.posts.findOneAndUpdate({_id:ObjectId("postId"),$or:[{type:"blog"},{author:123}]},{$set:{content:"blog78"}})
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!