I have a blog collection whose schema is something like below
{
content:String,
type:String,
author:Number
}
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"}})