- now there are such data structures, such as
in the collection class.
{
_id:ObjectId123456789...),
number:10,
students:[
{
studentId:ObjectId(123456789...)/* studentid */
},
{
studentId:ObjectId(123456789...)/* studentid */
},
...
],
teacher:ObjectId(123456789...),
}
The data structure of
{
_id:ObjectId(123456789...);
name:"zhangsan",
age:20,
}
- now the requirement is to take out the student when querying class, so it needs to be associated with the query. I use the $lookup, of pipeline, but it seems that I can only query the attributes at the next level of class, such as the association query teacher
.
db.class.aggregate([
{
$lookup:
{
from: "teacher",
localField: "teacher",
foreignField: "_id",
as: "teacher"
}
}
])
- but studentId, under student cannot be implemented. I have tried
db.class.aggregate([
{
$unwind:students,
},
{
$lookup:
{
from: "student",
localField: "students.studentId",
foreignField: "_id",
as: "student",/* student */
}
}
])
- I hope there is a boss who can solve the problem! Cannot change data structure