【问题标题】:Count data from subdocuments in mongodb with mongoose使用 mongoose 计算 mongodb 中子文档的数据
【发布时间】:2016-11-30 21:13:27
【问题描述】:

我正在尝试使用嵌套子文档对数据进行计数,但我无法理解如何获得我想要的(如果可能的话)。

我有以下用于约会列表的猫鼬模式:

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

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

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

从预约列表中,我希望报告指标以了解每个诊所不同类型预约的数量。

到目前为止,无论诊所使用以下聚合,我都只能获得每种预约类型的计数:

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 

但我想得到如下:

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

【问题讨论】:

    标签: mongodb mongoose-schema


    【解决方案1】:

    下面的聚合查询会给你类似的结果:

    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 中的 count: 4 与什么相关?

    【讨论】:

      猜你喜欢
      • 2021-08-15
      • 2015-05-25
      • 1970-01-01
      • 2021-04-10
      • 2013-09-04
      • 2018-01-26
      • 2021-05-11
      • 1970-01-01
      相关资源
      最近更新 更多