MongoDBを使用して、複数のアレイを1つの巨大なアレイに集約しますか?
複数の配列を1つの配列に集約するには、MongoDBの$projectを使用します。ドキュメントを使用してコレクションを作成しましょう-
> db.demo119.insertOne(
... {
... "_id": 101,
... "WebDetails": [
... {
... "ImagePath": "/all/image1",
... "isCorrect": "false"
... },
... {
... "ImagePath": "/all/image2",
... "isCorrect": "true"
... }
... ],
... "ClientDetails": [
... {
... "Name": "Chris",
... "isCorrect": "false"
... },
... {
... "Name": "David",
... "isCorrect": "true"
... }
... ]
... }
... );
{ "acknowledged" : true, "insertedId" : 101 } find()メソッドを使用してコレクションからすべてのドキュメントを表示する-
> db.demo119.find();
これにより、次の出力が生成されます-
{
"_id" : 101, "WebDetails" : [
{ "ImagePath" : "/all/image1", "isCorrect" : "false" },
{ "ImagePath" : "/all/image2", "isCorrect" : "true" } ], "ClientDetails" : [
{ "Name" : "Chris", "isCorrect" : "false" }, { "Name" : "David", "isCorrect" : "true" }
]
} 以下は、MongoDB-
を使用して複数の配列を単一の配列に集約するためのクエリです。>
> db.demo119.aggregate([
... { "$project": {
... "AllDetails": {
... "$filter": {
... "input": {
... "$setUnion": [
... { "$ifNull": [ "$WebDetails", [] ] },
... { "$ifNull": [ "$ClientDetails", [] ] }
... ]
... },
... "as": "out",
... "cond": { "$eq": [ "$$out.isCorrect", "false" ] }
... }
... }
... }},
... { "$match": { "AllDetails.0": { "$exists": true } } }
... ]) これにより、次の出力が生成されます-
{ "_id" : 101, "AllDetails" : [ { "ImagePath" : "/all/image1", "isCorrect" : "false" }, { "Name" : "Chris", "isCorrect" : "false" } ] } -
MongoDBは、配列を持つ1つのレコードを新しいコレクションの複数のレコードに変換しますか?
このために、aggregate()および$unwindとともに$outを使用できます。ドキュメントを使用してコレクションを作成しましょう- > db.demo757.insertOne( ... { ... "id": 101, ... "Name": ["John", "Bob", "Chris"] ... } ... ); {  
-
MongoDB Aggregate groupの複数の結果?
複数の結果を集約するには、MongoDBで$groupを使用します。ドキュメントを使用してコレクションを作成しましょう- > db.demo765.insertOne( ... ... { ... Name:"John", ... "Category":"ComputerScience", ... "SubjectName":"MongoDB&quo