MongoDBのいくつかの配列要素でフィルタリングしますか?
このために、$elemMatch演算子を使用できます。 $ elemMatch演算子は、指定されたすべてのクエリ条件に一致する要素が少なくとも1つある配列フィールドを含むドキュメントと一致します。まず、ドキュメントを使用してコレクションを作成しましょう-
> db.filterBySeveralElementsDemo.insertOne(
"_id":100,
"StudentDetails": [
{
"StudentName": "John",
"StudentCountryName": "US",
},
{
"StudentName": "Carol",
"StudentCountryName": "UK"
}
]
}
);
{ "acknowledged" : true, "insertedId" : 100 }
> db.filterBySeveralElementsDemo.insertOne(
{
"_id":101,
"StudentDetails": [
{
"StudentName": "Sam",
"StudentCountryName": "AUS",
},
{
"StudentName": "Chris",
"StudentCountryName": "US"
}
]
}
);
{ "acknowledged" : true, "insertedId" : 101 } 以下は、find()メソッドを使用してコレクションからすべてのドキュメントを表示するためのクエリです-
> db.filterBySeveralElementsDemo.find().pretty();
これにより、次の出力が生成されます-
{
"_id" : 100,
"StudentDetails" : [
{
"StudentName" : "John",
"StudentCountryName" : "US"
},
{
"StudentName" : "Carol",
"StudentCountryName" : "UK"
}
]
}
{
"_id" : 101,
"StudentDetails" : [
{
"StudentName" : "Sam",
"StudentCountryName" : "AUS"
},
{
"StudentName" : "Chris",
"StudentCountryName" : "US"
}
]
} 以下は、いくつかの配列要素でフィルタリングするためのクエリです-
> db.filterBySeveralElementsDemo.find({ StudentDetails: { $elemMatch: { StudentName: 'Sam', StudentCountryName: 'AUS' }}}).pretty(); これにより、次の出力が生成されます-
{
"_id" : 101,
"StudentDetails" : [
{
"StudentName" : "Sam",
"StudentCountryName" : "AUS"
},
{
"StudentName" : "Chris",
"StudentCountryName" : "US"
}
]
} -
MongoDBを使用して埋め込みドキュメントの配列でクエリをフィルタリングしますか?
これには、MongoDBでaggregate()を使用します。ドキュメントを使用してコレクションを作成しましょう- > db.demo736.insertOne( ... { ... "_id": "101", ... "details1": [ ... { ... &q
-
述語に基づいて配列要素をフィルタリングするC#プログラム
配列を設定します。 int[] arr = { 40, 42, 12, 83, 75, 40, 95 }; Where句と述語を使用して、50を超える要素を取得します。 IEnumerable<int> myQuery = arr.AsQueryable() .Where((a, index) => a >= 50); 完全なコードを見てみましょう- 例 using System; using System.Linq; using System.Collections.Generic; public class Demo { public