Javascript 使用聚合框架从给定条目重新构造数据
我对MongoDB和Mongoose的开发相对来说是个新手 我有一个非常具体的问题,我不知道如何表达这个问题,以便它有意义 基本上,我的“约会”条目如下所示:Javascript 使用聚合框架从给定条目重新构造数据,javascript,node.js,mongodb,mongoose,aggregation-framework,Javascript,Node.js,Mongodb,Mongoose,Aggregation Framework,我对MongoDB和Mongoose的开发相对来说是个新手 我有一个非常具体的问题,我不知道如何表达这个问题,以便它有意义 基本上,我的“约会”条目如下所示: { "_id": "6064bb7a9724c40e19113a0c", "customer_id": "123", "date": "2021-04-30T18:00:00.000Z",
{
"_id": "6064bb7a9724c40e19113a0c",
"customer_id": "123",
"date": "2021-04-30T18:00:00.000Z",
"requested_services": [
{
"_id": "6064bb7a9724c40e19113a0d",
"service_ref_id": "60489ec10acf063c366be0c4",
"start_time": "2021-04-10T10:00:00.000Z",
"duration": 1,
"price": 170,
"notes": "Mireasa"
},
{
"_id": "6064bb7a9724c40e19113a0e",
"service_ref_id": "60489ec10acf063c366be0c4",
"start_time": "2021-04-10T10:00:00.000Z",
"duration": 1,
"price": 170,
"notes": "Nasa"
}
],
"is_archived": false
}
我想做的是聚合每个“约会”并返回一个数组,其中包含“请求的_服务”中每个条目的文档,如下所示:
{
“appointment_id”: "xxxxxxxxxxxxxxx",
"customer_id": "xxxxxxxxxxxxxxx",
"_id": "6064bb7a9724c40e19113a0d",
"service_ref_id": "60489ec10acf063c366be0c4",
"start_time": "2021-04-10T10:00:00.000Z",
"duration": 1,
"price": 170,
"notes": “Mireasa"
}
因此,基本上,对于每个约会中请求的每个服务,我想列出约会ID、客户ID,然后是请求的服务数据
我试着通过分组和投影来解决这个问题,但我真的不能把我的脑袋绕过去
有人能给我指出正确的方向吗?文档并没有真正帮助我,因为我甚至不知道要找什么
多谢各位 您必须请求的\u服务
数组,然后使用$project
操作符对其进行重塑。因此,$unwind
操作员解析请求的\u服务
数组,并且请求的\u服务
中的每个项目将接收其父项的数据
试试这个:
db.appoinces.aggregate([
{
$unwind:“$请求的\u服务”
},
{
$项目:{
约会id:“$\U id”,
客户标识:“$customer\u id”,
_id:“$请求的\u服务。\u id”,
服务参考号:“$请求的服务。服务参考号”,
开始时间:“$请求的服务。开始时间”,
持续时间:“$requested_services.duration”,
价格:“$required_services.price”,
备注:“$请求的服务。备注”
}
}
]);
输出
/*1*/
{
“约会id”:“6064bb7a9724c40e19113a0c”,
“客户id”:“123”,
“_id”:“6064bb7a9724c40e19113a0d”,
“服务参考id”:“60489ec10acf063c366be0c4”,
“开始时间”:“2021-04-10T10:00:00.000Z”,
“期限”:1,
“价格”:170,
“注释”:“Mireasa”
},
/* 2 */
{
“约会id”:“6064bb7a9724c40e19113a0c”,
“客户id”:“123”,
“_id”:“6064bb7a9724c40e19113a0e”,
“服务参考id”:“60489ec10acf063c366be0c4”,
“开始时间”:“2021-04-10T10:00:00.000Z”,
“期限”:1,
“价格”:170,
“注释”:“Nasa”
}
因此,根据Dheemanth Bhat的回答,我需要更深入地了解放松阶段,我最终解决了我的问题。我的模式的设计方式需要额外的放松:
router.route('/:id/appointments/read/all/:year/:month').get(verify, async (req, res) => {
User.aggregate()
// Separate each appointment into its own document
.unwind('appointments')
// Get all the appointments from the current user that match the selected date
.match({
_id: mongoose.Types.ObjectId(req.params.id),
'appointments.date': {
$gte: new Date(parseInt(req.params.year), parseInt(req.params.month) - 1, 1),
$lte: new Date(parseInt(req.params.year), parseInt(req.params.month) - 1, 31)
}
})
// Separate each requested service of each appointment into its own document
.unwind('appointments.requested_services')
// Format the result of each requested service
.project({
appointment_id: '$appointments._id',
_id: '$appointments.requested_services._id',
service_ref_id: '$appointments.requested_services.service_ref_id',
start_time: '$appointments.requested_services.start_time',
duration: '$appointments.requested_services.duration',
notes: '$appointments.requested_services.notes'
})
.sort({start_time: 1})
// Send the filtered appointments array
.exec((error, result) => {
if (error) return res.status(400).send('Something went wrong: ' + error);
res.json(result);
});
});
由于这是一个非常特殊的案例,我将代码留在这里,以防有人遇到类似的问题