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