MongoDBドキュメントのサブ配列から最高の値を見つけますか?
ドキュメント内のサブ配列から最大の値を見つけるには、集約フレームワークを使用できます。まず、ドキュメントを使用してコレクションを作成しましょう
> db.findHighestValueDemo.insertOne(
... {
... _id: 10001,
... "StudentDetails": [
... { "StudentName": "Chris", "StudentMathScore": 56},
... { "StudentName": "Robert", "StudentMathScore":47 },
... { "StudentName": "John", "StudentMathScore": 98 }]
... }
... );
{ "acknowledged" : true, "insertedId" : 10001 }
> db.findHighestValueDemo.insertOne(
... {
... _id: 10002,
... "StudentDetails": [
... { "StudentName": "Ramit", "StudentMathScore": 89},
... { "StudentName": "David", "StudentMathScore":76 },
... { "StudentName": "Bob", "StudentMathScore": 97 }
... ]
... }
... );
{ "acknowledged" : true, "insertedId" : 10002 } 以下は、find()メソッドを使用してコレクションからすべてのドキュメントを表示するためのクエリです
> db.findHighestValueDemo.find().pretty();
これにより、次の出力が生成されます
{
"_id" : 10001,
"StudentDetails" : [
{
"StudentName" : "Chris",
"StudentMathScore" : 56
},
{
"StudentName" : "Robert",
"StudentMathScore" : 47
},
{
"StudentName" : "John",
"StudentMathScore" : 98
}
]
}
{
"_id" : 10002,
"StudentDetails" : [
{
"StudentName" : "Ramit",
"StudentMathScore" : 89
},
{
"StudentName" : "David",
"StudentMathScore" : 76
},
{
"StudentName" : "Bob",
"StudentMathScore" : 97
}
]
} 以下は、ドキュメント内のサブ配列から最大値を見つけるためのクエリです
> db.findHighestValueDemo.aggregate([
... {$project:{"StudentDetails.StudentName":1, "StudentDetails.StudentMathScore":1}},
... {$unwind:"$StudentDetails"},
... {$sort:{"StudentDetails.StudentMathScore":-1}},
... {$limit:1}
... ]).pretty(); これにより、次の出力が生成されます
{
"_id" : 10001,
"StudentDetails" : {
"StudentName" : "John",
"StudentMathScore" : 98
}
} -
MongoDBドキュメントで特定の値を超える値を見つけますか?
特定の値を超える値を検索するには、MongoDBで$gteを使用する構文を次に示します- db.yourCollectionName.find({yourFieldName:{$gte:yourValue}}); ドキュメントを使用してコレクションを作成しましょう- > db.demo571.insertOne({"Price":140});{ "acknowledged" : true, "insertedId" : ObjectId("5e909b3439cfeaaf0b97b587&q
-
MySQLは最高値から最低値の順に並べますか?
最高値から最低値の順に並べ替えるには、ORDERBYDESCコマンド-を使用できます。 select *from yourTableName order by yourColumnName DESC; 低いものから高いものへの結果が必要な場合は、ORDERBYASCコマンド-を使用できます。 select *from yourTableName order by yourColumnName ASC; まずテーブルを作成しましょう- mysql> create table DemoTable ( Value int ); Query OK, 0 rows a