I have 3 models User
, Department
, and 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'
}
I'm trying to query tickets from a department in Ticket
Model, how can I do that?
example: Ticket.find({ author.department: req.user.department }).populate('author')
CodePudding user response:
db.tickets.aggregate([
{
"$lookup": {
"from": "users",
"localField": "author",
"foreignField": "_id",
"as": "author"
}
},
{
"$unwind": "$author"
},
{
$match: {
"author.department": req.user.department
}
}
])