2016-11-29 2 views
0

Ich versuche, Daten mit verschachtelten Subdokumente zu zählen, und ich bin nicht in der Lage zu verstehen, wie ich bekommen konnte, was ich will (und wenn es möglich ist).Zähldaten von Subdokumente in mongodb mit Mungo

Ich habe folgendes Mungo Schema, das für eine Liste von Terminen verwendet wird:

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 } 
 
);

Aus dieser Liste von Terminen, ich bin auf der Suche Metriken zu berichten, die Anzahl der verschiedenen wissen Art der Termine pro Klinik.

Bisher bin ich nur in der Lage eine Zählung pro Art von Terminen zu erhalten unabhängig von der Klinik mit der folgenden Aggregation:

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

Dies kehrt mir die folgenden Ergebnisse:

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

Aber was würde Ich mag wäre wie folgt zu erhalten:

{ 
 
\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 } 
 
}

Antwort

0

Die folgende Aggregationsanfrage wird Ihnen ein ähnliches Ergebnis:

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

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

Beispiel Ausgabe:

{ 
     "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" 
} 

Was macht Ihr count: 4 innerhalb count beziehen sich auf?