I have the following query:
const productTypes = await ProductTypes.find(
{type},
{
style: {
$in: styles
}
}
);
This is my styles array:
styles [ 'mom' ]
And I get this error:
MongoServerError: Expression $in takes exactly 2 arguments. 1 were passed in.
CodePudding user response:
At first glance, it looks like your query is wrong. You need so move the style query into the first param {type}
.
You can check out a live query here
Something like this:
const productTypes = await ProductTypes.find({
type,
style: {
$in: styles // ["mom"]
}
});
This is the same stuff from the live demo... putting it here for archival purposes.
Test Database
[
{
"type": "a",
"styles": [
"mom",
"dad",
"sister"
]
},
{
"type": "b",
"styles": [
"mom",
"brother",
"dad"
]
}
]
Query
db.collection.find({
type: "a",
styles: {
$in: [
"mom"
]
}
})
Result
[
{
"_id": ObjectId("5a934e000102030405000000"),
"styles": [
"mom",
"dad",
"sister"
],
"type": "a"
}
]