MongoDBで個別のレコード値を取得しますか?
MongoDBのdistinct()メソッドを使用して、個別のレコード値を取得できます。構文は次のとおりです-
db.yourCollectionName.distinct(“yourFieldName”);
上記の構文を理解するために、ドキュメントを使用してコレクションを作成しましょう。ドキュメントを使用してコレクションを作成するためのクエリは次のとおりです-
> db.distinctRecordDemo.insertOne({"StudentId":1,"StudentName":"John","StudentAge":21});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c77a78299b97a65744c1b50")
}
> db.distinctRecordDemo.insertOne({"StudentId":2,"StudentName":"John","StudentAge":22});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c77a78b99b97a65744c1b51")
}
> db.distinctRecordDemo.insertOne({"StudentId":3,"StudentName":"Carol","StudentAge":21});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c77a79a99b97a65744c1b52")
}
> db.distinctRecordDemo.insertOne({"StudentId":4,"StudentName":"Carol","StudentAge":26});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c77a7a499b97a65744c1b53")
}
> db.distinctRecordDemo.insertOne({"StudentId":5,"StudentName":"Sam","StudentAge":24});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c77a7b499b97a65744c1b54")
}
> db.distinctRecordDemo.insertOne({"StudentId":6,"StudentName":"Mike","StudentAge":27});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c77a7c799b97a65744c1b55")
}
> db.distinctRecordDemo.insertOne({"StudentId":7,"StudentName":"Sam","StudentAge":28});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c77a7d399b97a65744c1b56")
} find()メソッドを使用して、コレクションのすべてのドキュメントを表示します。クエリは次のとおりです-
> db.distinctRecordDemo.find().pretty();
出力は次のとおりです。
{
"_id" : ObjectId("5c77a78299b97a65744c1b50"),
"StudentId" : 1,
"StudentName" : "John",
"StudentAge" : 21
}
{
"_id" : ObjectId("5c77a78b99b97a65744c1b51"),
"StudentId" : 2,
"StudentName" : "John",
"StudentAge" : 22
}
{
"_id" : ObjectId("5c77a79a99b97a65744c1b52"),
"StudentId" : 3,
"StudentName" : "Carol",
"StudentAge" : 21
}
{
"_id" : ObjectId("5c77a7a499b97a65744c1b53"),
"StudentId" : 4,
"StudentName" : "Carol",
"StudentAge" : 26
}
{
"_id" : ObjectId("5c77a7b499b97a65744c1b54"),
"StudentId" : 5,
"StudentName" : "Sam",
"StudentAge" : 24
}
{
"_id" : ObjectId("5c77a7c799b97a65744c1b55"),
"StudentId" : 6,
"StudentName" : "Mike",
"StudentAge" : 27
}
{
"_id" : ObjectId("5c77a7d399b97a65744c1b56"),
"StudentId" : 7,
"StudentName" : "Sam",
"StudentAge" : 28
} これは、MongoDBで個別のレコード値を取得するためのクエリです。
ケース1 −ここでのフィールドは「StudentName」です。
クエリは次のとおりです-
> db.distinctRecordDemo.distinct("StudentName"); 以下は、StudentName-
の個別のレコードを表示する出力です。[ "John", "Carol", "Sam", "Mike" ]
ケース2 −ここでのフィールドは「StudentAge」です。
クエリは次のとおりです-
> db.distinctRecordDemo.distinct("StudentAge"); 以下は、StudentAge-
の個別のレコードを表示する出力です。[ 21, 22, 26, 24, 27, 28 ]
-
MongoDBコレクションから一意の値を取得するにはどうすればよいですか?
一意の値を取得して重複を無視するには、MongoDBでdistinct()を使用します。個別の()は、単一のコレクション全体で指定されたフィールドの個別の値を検索し、結果を配列で返します。 ドキュメントを使用してコレクションを作成しましょう- > db.demo704.insertOne({"LanguageCode":"hi"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea6ee
-
MongoDBで個別の要素の最大値を取得する方法
個別の要素の最大値を取得するには、MongoDBのaggregate()で$sortと$groupを使用します。ドキュメントを使用してコレクションを作成しましょう- > db.demo750.insertOne({id:101,value:50}); { "acknowledged" : true, "insertedId" : ObjectId("5eae74b2a930c785c834e566") } > db.demo750.insertOne({id:102,va