从ref对象中查询



我有三个型号User,DepartmentTicket

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
}
}
])

测试在这里

最新更新