gpt4 book ai didi

mongodb - 聚合和展开数组,但保留顶级 key

转载 作者:行者123 更新时间:2023-12-04 10:42:27 25 4
gpt4 key购买 nike

假设我的集合中有以下文档 Classes Collecton


{
"_id": ObjectId("5df58d45244a850d54b922c8"),
"mentors" : {
"numOfMentors" : NumberInt(1),
"mentorList" : [
ObjectId("5c9ba636347bb645e0865283")
]
},
"lessons": [
{
"_id": ObjectId("5db221be211d7b68ac8be618"),
"mentorData": {
"objective": "Ensuring students that making mistakes is normal and that it is a part of life",
"task": "Post a video explaining obstacles that you had to overcome as a programmer",
"dueDate": "2019-12-14T15:26:10.000+0000"
},
"studentData": {
"objective": "Learning that failures help you grow",
"task": "Post a video explaining obstacles that you have overcame",
"dueDate": "2020-01-14T22:26:10.000+0000" <---- CHECKING THIS DATE
},
"title": "How to overcome obstacles",
"comments": []
}
]
}

我正在汇总如下:


Class.aggregate([
{ $match: { "students.studentList": req.user._id } },
{ $unwind: "$lessons" },
{
$addFields: {
date: {
$dateToString: {
format: "%Y-%m-%d",
date: "$lessons.studentData.dueDate"
}
}
}
},
{
$match: {
$and: [
{ date: { $gte: startDate } },
{ date: { $lte: endDate } }
]
}
},
{ $group: { _id: "$_id", lessons: { $push: "$lessons" } } }
])

返回这个...

 {
"lessonImage": {
"url": "https://stemsandbox.blob.core.windows.net/stemuli/lesson-picture-a406bd19-0677-4b60-909e-7de2ca3c6f93.jpg",
"originalname": "nintendo-switch-console.jpg",
"mimetype": "image/jpeg",
"blobName": "lesson-picture-a406bd19-0677-4b60-909e-7de2ca3c6f93.jpg",
"container": "stemuli",
"blob": "lesson-picture-a406bd19-0677-4b60-909e-7de2ca3c6f93.jpg",
"size": "147469",
"etag": "\"0x8D797B8F1EC9C39\"",
"createdOn": "2020-01-12T23:41:28.588Z"
},
"_id": "5db221be211d7b68ac8be619",
"mentorData": {
"objective": "Learn to make a single web page web app",
"task": "Create a short video instructing how to setup environment",
"dueDate": "2019-02-02T22:26:10.000Z"
},
"studentData": {
"objective": "Program a single page web app in React",
"task": "Program a single page web app in React and submit by october 30th",
"dueDate": "2020-01-22T22:26:10.000Z"
},
"title": "Learning to program in React",

}

我希望它是这样的备注 mentors 字段

 {
"lessonImage": {
"url": "https://stemsandbox.blob.core.windows.net/stemuli/lesson-picture-a406bd19-0677-4b60-909e-7de2ca3c6f93.jpg",
"originalname": "nintendo-switch-console.jpg",
"mimetype": "image/jpeg",
"blobName": "lesson-picture-a406bd19-0677-4b60-909e-7de2ca3c6f93.jpg",
"container": "stemuli",
"blob": "lesson-picture-a406bd19-0677-4b60-909e-7de2ca3c6f93.jpg",
"size": "147469",
"etag": "\"0x8D797B8F1EC9C39\"",
"createdOn": "2020-01-12T23:41:28.588Z"
},
"_id": "5db221be211d7b68ac8be619",
"mentorData": {
"objective": "Learn to make a single web page web app",
"task": "Create a short video instructing how to setup environment",
"dueDate": "2019-02-02T22:26:10.000Z"
},
"studentData": {
"objective": "Program a single page web app in React",
"task": "Program a single page web app in React and submit by october 30th",
"dueDate": "2020-01-22T22:26:10.000Z"
},
"title": "Learning to program in React",
"classId": "5e1baea87fcee8639cbce29d",\
//FIELD BELOW ADDED
"mentors": [
ObjectId("5c9ba636347bb645e0865283")
]
}

最佳答案

您可以将$push 添加到您的$group,然后运行$reduce作为展平数组数组的下一阶段:

{ $group: { _id: "$_id", lessons: { $push: "$lessons" }, mentors: { $push: "$mentors.mentorList" } } },
{ $addFields: { mentors: { $reduce: { input: "$mentors", initialValue: [], in: { $setUnion: [ "$$value", "$$this" ] } } } } }

Mongo Playground

编辑:$setUnion将删除任何潜在的重复项

关于mongodb - 聚合和展开数组,但保留顶级 key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59865893/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com