2016-11-29 7 views
0

ネストされたサブ文書でデータを数えようとしていますが、私が望むものを得る方法がわかりません。mongooseのmongodbのサブ文書からのデータを集計します。

私は予定のリストに使用され、次のマングーススキーマを持っている:

予定のリストから

var AppointmentSchema = new mongoose.Schema(
 
\t { 
 
\t \t clinic: { 
 
\t \t \t type: mongoose.Schema.Types.ObjectId, 
 
\t \t \t ref: 'Clinic', 
 
\t \t \t required: true, 
 
\t \t }, 
 
\t \t type: { 
 
\t \t \t type: mongoose.Schema.Types.ObjectId, 
 
\t \t \t ref: 'AppointmentType', 
 
\t \t \t required: true, 
 
\t \t } 
 
\t } 
 
); 
 

 
var ClinicSchema = new mongoose.Schema(
 
\t { 
 
\t \t name: { 
 
\t \t \t type: String, 
 
\t \t \t maxlength: 25, 
 
\t \t \t required: true, 
 
\t \t } 
 
\t } 
 
); 
 

 
var AppointmentTypeSchema = new mongoose.Schema(
 
\t { 
 
\t \t name: { 
 
\t \t \t type: String, 
 
\t \t \t minlength: 2, 
 
\t \t \t maxlength: 25, 
 
\t \t \t required: true, 
 
\t \t } 
 
\t } 
 
);

、私は別の番号を知る指標を報告していますよ診療ごとの予約の種類。

は、これまでのところ私は、以下の集約を使用して関係なく、診療所を予定の種類ごとの数を取得することしかできています:

db.appointments.aggregate(
 
    [ 
 
     { 
 
      $group: { 
 
       _id: '$type', //$type is the column name in collection 
 
       count: {$sum: 1} 
 
      } 
 
     }, 
 
     { 
 
      $sort: { count: -1 } 
 
     } 
 
    ] 
 
);

これは私に次のような結果を返して

{ "_id" : ObjectId("5838ef21b19aee730b8ae6c8"), "count" : 5 } 
 
{ "_id" : ObjectId("5838efa4d695cb7839672417"), "count" : 3 } 
 
{ "_id" : ObjectId("5838efb4d695cb7839672419"), "count" : 3

しかし、私は次のようになります取得したいのですがどのような:

{ 
 
\t { 
 
\t \t id: 1, 
 
\t \t name: "Name of the cliniC#1", 
 
\t \t count: [ 
 
\t \t \t { 
 
\t \t \t \t id: 10, 
 
\t \t \t \t name: "Appointment Type #10", 
 
\t \t \t \t count: 4, 
 
\t \t \t }, 
 
\t \t \t { 
 
\t \t \t \t id: 20, 
 
\t \t \t \t name: "Appointment Type #20", 
 
\t \t \t \t count: 1, 
 
\t \t \t } 
 
\t \t ] 
 
\t }, 
 
\t { 
 
\t \t id: 2, 
 
\t \t name: "Name of the cliniC#2", 
 
\t \t count: [ 
 
\t \t \t { 
 
\t \t \t \t id: 10, 
 
\t \t \t \t name: "Appointment Type #10", 
 
\t \t \t \t count: 5, 
 
\t \t \t }, 
 
\t \t \t { 
 
\t \t \t \t id: 20, 
 
\t \t \t \t name: "Appointment Type #20", 
 
\t \t \t \t count: 2, 
 
\t \t \t } 
 
\t \t ] 
 
\t } 
 
}

答えて

0

は、以下の集計クエリは、あなたにも同様の結果が得られます:

db.appointments.aggregate(
    [ 
     { 
      $group: { 
       _id: "$clinic", 
       count: { $push: "$type" } 
      } 

     }, 
     { 
      $project:{ 
       _id: 0, 
       id: "$_id._id", 
       name: "$_id.name", 
       "count": 1 
      } 
     } 
    ] 
).pretty(); 

出力例:

{ 
     "count" : [ 
       { 
         "_id" : ObjectId("583ecd06401ce04a0ca7e170"), 
         "name" : "Appointment Type 1" 
       }, 
       { 
         "_id" : ObjectId("583ecd09401ce04a0ca7e171"), 
         "name" : "Appointment Type 2" 
       }, 
       { 
         "_id" : ObjectId("583ecd0c401ce04a0ca7e172"), 
         "name" : "Appointment Type 3" 
       } 
     ], 
     "id" : ObjectId("583eccf6401ce04a0ca7e16d"), 
     "name" : "Clinic 1" 
} 
{ 
     "count" : [ 
       { 
         "_id" : ObjectId("583ecd06401ce04a0ca7e170"), 
         "name" : "Appointment Type 1" 
       }, 
       { 
         "_id" : ObjectId("583ecd0c401ce04a0ca7e172"), 
         "name" : "Appointment Type 3" 
       } 
     ], 
     "id" : ObjectId("583eccfd401ce04a0ca7e16e"), 
     "name" : "Clinic 2" 
} 
{ 
     "count" : [ 
       { 
         "_id" : ObjectId("583ecd06401ce04a0ca7e170"), 
         "name" : "Appointment Type 1" 
       } 
     ], 
     "id" : ObjectId("583ecd01401ce04a0ca7e16f"), 
     "name" : "Clinic 3" 
} 

count: 4countにありますか?

関連する問題