特定のキーを持つレコードを選択するためのMongoDBクエリ?
特定のキーを持つレコードを選択するには、$exists演算子を使用できます。構文は次のとおりです-
db.yourCollectionName.find( { yourFieldName: { $exists : true } } ).pretty(); 上記の構文を理解するために、ドキュメントを使用してコレクションを作成しましょう。ドキュメントを使用してコレクションを作成するためのクエリは次のとおりです-
> db.selectRecordsHavingKeyDemo.insertOne({"StudentName":"John","StudentAge":21,"StudentMathMarks":78});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c8b7be780f10143d8431e0f")
}
> db.selectRecordsHavingKeyDemo.insertOne({"StudentName":"Carol","StudentMathMarks":89});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c8b7bfc80f10143d8431e10")
}
> db.selectRecordsHavingKeyDemo.insertOne({"StudentName":"Sam","StudentAge":26,"StudentMathMarks":89});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c8b7c1280f10143d8431e11")
}
> db.selectRecordsHavingKeyDemo.insertOne({"StudentName":"Sam","StudentMathMarks":98});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c8b7c2180f10143d8431e12")
} find()メソッドを使用して、コレクションのすべてのドキュメントを表示します。クエリは次のとおりです-
> db.selectRecordsHavingKeyDemo.find().pretty();
以下は出力です-
{
"_id" : ObjectId("5c8b7be780f10143d8431e0f"),
"StudentName" : "John",
"StudentAge" : 21,
"StudentMathMarks" : 78
}
{
"_id" : ObjectId("5c8b7bfc80f10143d8431e10"),
"StudentName" : "Carol",
"StudentMathMarks" : 89
}
{
"_id" : ObjectId("5c8b7c1280f10143d8431e11"),
"StudentName" : "Sam",
"StudentAge" : 26,
"StudentMathMarks" : 89
}
{
"_id" : ObjectId("5c8b7c2180f10143d8431e12"),
"StudentName" : "Sam",
"StudentMathMarks" : 98
} これは、特定のキーを持つレコードを選択するためのクエリです-
> db.selectRecordsHavingKeyDemo.find( { StudentMathMarks : { $exists : true } } ).pretty(); 以下は出力です-
{
"_id" : ObjectId("5c8b7be780f10143d8431e0f"),
"StudentName" : "John",
"StudentAge" : 21,
"StudentMathMarks" : 78
}
{
"_id" : ObjectId("5c8b7bfc80f10143d8431e10"),
"StudentName" : "Carol",
"StudentMathMarks" : 89
}
{
"_id" : ObjectId("5c8b7c1280f10143d8431e11"),
"StudentName" : "Sam",
"StudentAge" : 26,
"StudentMathMarks" : 89
}
{
"_id" : ObjectId("5c8b7c2180f10143d8431e12"),
"StudentName" : "Sam",
"StudentMathMarks" : 98
} -
上位10レコードを選択するためのMySQLクエリ?
トップ10レコードを選択するには、MySQLでLIMITを使用します。まずテーブルを作成しましょう- mysql> create table DemoTable -> ( -> PageNumber text -> ); Query OK, 0 rows affected (2.50 sec) 挿入コマンド-を使用して、テーブルにいくつかのレコードを挿入します mysql> insert into DemoTable values('Page-1'); Query OK, 1 row affected (0.46 sec) mysql>
-
下位nレコードを選択するためのMySQLクエリ
まずテーブルを作成しましょう- mysql> create table DemoTable724 (Value int); Query OK, 0 rows affected (0.79 sec) 挿入コマンド-を使用して、テーブルにいくつかのレコードを挿入します mysql> insert into DemoTable724 values(101); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable724 values(183); Query OK, 1 row affected (0.15