我有以下查詢:
const productTypes = await ProductTypes.find(
{type},
{
style: {
$in: styles
}
}
);
這是我的樣式陣列:
styles [ 'mom' ]
我得到這個錯誤:
MongoServerError: Expression $in takes exactly 2 arguments. 1 were passed in.
uj5u.com熱心網友回復:
乍一看,您的查詢似乎是錯誤的。您需要將樣式查詢移動到第一個引數{type}中。
您可以在此處查看實時查詢
像這樣的東西:
const productTypes = await ProductTypes.find({
type,
style: {
$in: styles // ["mom"]
}
});
這與現場演示中的內容相同……出于存檔目的將其放在這里。
測驗資料庫
[
{
"type": "a",
"styles": [
"mom",
"dad",
"sister"
]
},
{
"type": "b",
"styles": [
"mom",
"brother",
"dad"
]
}
]
詢問
db.collection.find({
type: "a",
styles: {
$in: [
"mom"
]
}
})
結果
[
{
"_id": ObjectId("5a934e000102030405000000"),
"styles": [
"mom",
"dad",
"sister"
],
"type": "a"
}
]
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/430451.html
標籤:mongodb
