MongoDBアグリゲートを使用して配列の値のみを取得するにはどうすればよいですか?
ドキュメントを使用してコレクションを作成しましょう-
> db.demo411.insertOne(
... {
... "Information" : [
... {
... "Name1" : "Chris",
... "Name2" : "David"
... },
... {
... "Name1" : "John",
... "Name2" : "John"
... }
... ]
... }
... );
{
"acknowledged" : true,
"insertedId" : ObjectId("5e70f19715dc524f70227682")
} find()メソッドを使用してコレクションからすべてのドキュメントを表示する-
> db.demo411.find();
これにより、次の出力が生成されます-
{ "_id" : ObjectId("5e70f19715dc524f70227682"), "Information" : [ { "Name1" : "Chris", "Name2" : "David" }, { "Name1" : "John", "Name2" : "John" } ] } 以下は、配列内の値のみを取得するためのクエリです-
> db.demo411.aggregate(
... [
... {$project : {
... _id : 0,
... Information : {$map : {input : "$Information", as : "out", in : ["$$out.Name1", "$$out.Name2"]}}
... }
... }
... ]
... ) これにより、次の出力が生成されます-
{ "Information" : [ [ "Chris", "David" ], [ "John", "John" ] ] } -
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