[英]$Push if Object doesn't already exist
I have an array of students
in my Documents VirtualClass
and my goal is to create a roster with no duplication of students.我的 Documents
VirtualClass
有一系列students
,我的目标是创建一个没有重复学生的名册。
I want to match the VirtualClass
with the teacher and then $push
the student into an array.我想将
VirtualClass
与老师匹配,然后$push
学生$push
放入一个数组中。
Virtual Class Schema虚拟类模式
const VirtualClassSchema = new Schema({
name: { type: String, required: true },
descriptionHeading: { type: String, required: true },
room: { type: String },
teacher: {
userId: { type: Schema.Types.ObjectId, ref: "User" },
name: { type: String },
profile_picture: { type: String }
},
students: [
{
userId: { type: Schema.Types.ObjectId, ref: "User" },
name: { type: String },
profile_picture: { type: String }
}
],
...
My current query is as follows我目前的查询如下
VirtualClass.aggregate([
{ $match: { "teacher.userId": user._id } },
{
$group: {
_id: null,
students: { $addToSet: "$students" }
}
}
])
Which returns:返回:
[
{
"_id": null,
"students": [
[
{
"_id": "5e84d1a1ab3ebf54283b8cb2",
"userId": "5dd27452592f600900235945",
"name": "student zero",
"profile_picture": "https://productionstemulistorage.blob.core.windows.net/stemuli/profile-picture-6e609f3b-44cb-44c0-888a-1b6767e3072d"
}
],
[
{
"_id": "5e84d1a1ab3ebf54283b8cb4",
"userId": "5dd27452592f600900235945",
"name": "student zero",
"profile_picture": "https://productionstemulistorage.blob.core.windows.net/stemuli/profile-picture-6e609f3b-44cb-44c0-888a-1b6767e3072d"
}
]
]
}
]
Expected results:预期成绩:
_id: null,
"students":
[
{
"_id": "5e84d1a1ab3ebf54283b8cb4",
"userId": "5dd27452592f600900235945",
"name": "student zero",
"profile_picture": "https://productionstemulistorage.blob.core.windows.net/stemuli/profile-picture-6e609f3b-44cb-44c0-888a-1b6767e3072d"
}
]
Thank you!谢谢!
You could use exclusion to remove the id from the document before sending it to $addToSet.在将文档发送到 $addToSet 之前,您可以使用排除从文档中删除该 ID。 Also your student is array so you would need
$unwind
.你的学生也是数组,所以你需要
$unwind
。
Something like就像是
VirtualClass.aggregate([
{$match:{"teacher.userId": user._id}},
{$project:{"students._id": 0}},
{$unwind: "$students"},
{$group:{
_id: null,
students: {$addToSet:"$students"}
}}
])
example here - https://mongoplayground.net/p/zULrmihM03z这里的例子 - https://mongoplayground.net/p/zULrmihM03z
Output输出
[
{
"_id": null,
"students": [
{
"name": "student zero",
"profile_picture": "https://productionstemulistorage.blob.core.windows.net/stemuli/profile-picture-6e609f3b-44cb-44c0-888a-1b6767e3072d",
"userId": "5dd27452592f600900235945"
}
]
}
]
Another alternative would be to set _id to false so mongoose will not generate any id for student.另一种选择是将 _id 设置为 false,这样猫鼬就不会为学生生成任何 id。
Something like就像是
students: [
{
userId: { type: Schema.Types.ObjectId, ref: "User" },
name: { type: String },
profile_picture: { type: String },
_id:false
}
],
You can then use aggregation query without the exclusion.然后,您可以使用没有排除的聚合查询。
VirtualClass.aggregate([
{$match:{"teacher.userId": user._id}},
{$unwind: "$students"},
{$group:{
_id: null,
students: {$addToSet:"$students"}
}}
])
You need to $unwind
and then $project
to remove _id
before $group
in your aggregate function.您需要先
$unwind
然后$project
在聚合函数中删除$group
之前的_id
。
Code:代码:
[
{
"$match": {
"teacher.userId": 1
}
},
{
"$unwind": "$students"
},
{
"$project": {
"students._id": 0
}
},
{
"$group": {
"_id": null,
"students": {
"$addToSet": "$students"
}
}
}
]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.