Home > front end >  Why is my mongo query using the $in operator producting an error?
Why is my mongo query using the $in operator producting an error?

Time:05-07

The following query is erroring out:

const pastOrders = await Order.find(
        {user: userId},
        {status: {$in: ["delivered", "refunded"]}}
    ).populate({
        path: 'items.product',
        model: 'Product'
    });

The error:

 MongoServerError: $in requires an array as a second argument, found: string

The schema of status is as follows:

status: {
  type: String,
  default: 'pending',      
  required: true
},

CodePudding user response:

It should be:

.find({user: userId, status: {$in: ["delivered", "refunded"]}})

No need to for the },{ after userId

  • Related