どのMongoDBクエリが配列内で同じ値を複数回検出しますか?
$where演算子をスクリプトと一緒に使用できます。
まず、ドキュメントを使用してコレクションを作成しましょう-
> dbsameValueMultipleTimesDemoinsertOne(
{
"ListOfPrice":[
{"Price": 110},
{"Price":130},
{"Price": 145}
]
}
);
{
"acknowledged" : true,
"insertedId" : ObjectId("5cefc4e6ef71edecf6a1f6b9")
}
> dbsameValueMultipleTimesDemoinsertOne(
{
"ListOfPrice":[
{"Price": 110},
{"Price":178},
{"Price": 110}
]
}
);
{
"acknowledged" : true,
"insertedId" : ObjectId("5cefc4e7ef71edecf6a1f6ba")
} 以下は、find()メソッドを使用してコレクションからすべてのドキュメントを表示するためのクエリです-
> dbsameValueMultipleTimesDemofind()pretty();
出力
{
"_id" : ObjectId("5cefc4e6ef71edecf6a1f6b9"),
"ListOfPrice" : [
{
"Price" : 110
},
{
"Price" : 130
},
{
"Price" : 145
}
]
}
{
"_id" : ObjectId("5cefc4e7ef71edecf6a1f6ba"),
"ListOfPrice" : [
{
"Price" : 110
},
{
"Price" : 178
},
{
"Price" : 110
}
]
} 以下は、配列内で同じ値を複数回検索するためのクエリです-
> dbsameValueMultipleTimesDemofind({
"$where": function() {
return thisListOfPricefilter(function(p) {
return pPrice == 110;
})length > 1;
}
}) 出力
{ "_id" : ObjectId("5cefc4e7ef71edecf6a1f6ba"), "ListOfPrice" : [ { "Price" : 110 }, { "Price" : 178 }, { "Price" : 110 } ] } -
idがドキュメントフィールドの配列値と等しい場合に除外するMongoDBクエリ
このために、$inと一緒に$notを使用します。ドキュメントを使用してコレクションを作成しましょう- [ { id: "101", subjectid: [ "102" ] }, { id: "102", &nb
-
MongoDBで配列をクエリして、特定の値をフェッチします
配列から特定の値をフェッチするには、$ projectとともにaggregate()を使用します。ドキュメントを使用してコレクションを作成しましょう- > db.demo761.insertOne( ... { ... "details": [ ... { ... "student": { ... &nb