'Why is my mongo query using the $in operator producting an error?
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
},
Solution 1:[1]
It should be:
.find({user: userId, status: {$in: ["delivered", "refunded"]}})
No need to for the },{
after userId
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | nimrod serok |