MongoDBで複数のキーを使用して「個別」を効率的に実行するにはどうすればよいですか?
集約フレームワークを使用して、複数のキーを使用して個別に実行できます。
概念を理解するために、ドキュメントを使用してコレクションを作成しましょう。ドキュメントを使用してコレクションを作成するためのクエリは次のとおりです-
> db.distinctWithMultipleKeysDemo.insertOne({"StudentName":"Mike","StudentAge":22,"StudentMathMarks":56});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c7f74488d10a061296a3c53")
}
> db.distinctWithMultipleKeysDemo.insertOne({"StudentName":"Mike","StudentAge":22,"StudentMathMarks":56});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c7f744b8d10a061296a3c54")
}
> db.distinctWithMultipleKeysDemo.insertOne({"StudentName":"Bob","StudentAge":23,"StudentMathMarks":45});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c7f74598d10a061296a3c55")
}
> db.distinctWithMultipleKeysDemo.insertOne({"StudentName":"Bob","StudentAge":23,"StudentMathMarks":45});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c7f745e8d10a061296a3c56")
}
> db.distinctWithMultipleKeysDemo.insertOne({"StudentName":"Carol","StudentAge":27,"StudentMathMarks":54});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c7f74688d10a061296a3c57")
} find()メソッドを使用して、コレクションのすべてのドキュメントを表示します。クエリは次のとおりです-
> db.distinctWithMultipleKeysDemo.find().pretty();
以下は出力です-
{
"_id" : ObjectId("5c7f74488d10a061296a3c53"),
"StudentName" : "Mike",
"StudentAge" : 22,
"StudentMathMarks" : 56
}
{
"_id" : ObjectId("5c7f744b8d10a061296a3c54"),
"StudentName" : "Mike",
"StudentAge" : 22,
"StudentMathMarks" : 56
}
{
"_id" : ObjectId("5c7f74598d10a061296a3c55"),
"StudentName" : "Bob",
"StudentAge" : 23,
"StudentMathMarks" : 45
}
{
"_id" : ObjectId("5c7f745e8d10a061296a3c56"),
"StudentName" : "Bob",
"StudentAge" : 23,
"StudentMathMarks" : 45
}
{
"_id" : ObjectId("5c7f74688d10a061296a3c57"),
"StudentName" : "Carol",
"StudentAge" : 27,
"StudentMathMarks" : 54
} これは、複数のキーを使用して個別に実行するためのクエリです-
> c = db.distinctWithMultipleKeysDemo;
test.distinctWithMultipleKeysDemo
> myResult = c.aggregate( [ {"$group": { "_id": { StudentName:"$StudentName", StudentAge: "$StudentAge" } } } ] ); 以下は出力です-
{ "_id" : { "StudentName" : "Carol", "StudentAge" : 27 } }
{ "_id" : { "StudentName" : "Bob", "StudentAge" : 23 } }
{ "_id" : { "StudentName" : "Mike", "StudentAge" : 22 } } -
MongoDBで複数のドキュメントをマージする方法は?
MongoDBで複数のドキュメントをマージするには、aggregate()を使用します。ドキュメントを使用してコレクションを作成しましょう- > db.demo436.insertOne( ... { ... "_id" : "101", ... "Name": "Chris", ... "details" : [ ... &
-
MongoDBで配列内のアイテムをカウントする方法は?
配列内のアイテムをカウントするには、lengthを使用します。ドキュメントを使用してコレクションを作成しましょう- > db.demo440.insertOne( ... { ... "Name":"Chris", ... "ListOfFriends":["John","Sam","Mike"] ... } ... ); { &nbs