我有三个型号User
,Department
和Ticket
。
const TicketSchema = mongoose.Schema({
author: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
}
const UserSchema = mongoose.Schema({
department: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Department'
}
我正试图从Ticket
模型的部门查询门票,我该怎么做?
示例:Ticket.find({ author.department: req.user.department }).populate('author')
db.tickets.aggregate([
{
"$lookup": {
"from": "users",
"localField": "author",
"foreignField": "_id",
"as": "author"
}
},
{
"$unwind": "$author"
},
{
$match: {
"author.department": req.user.department
}
}
])
测试在这里