Hi im really struggling finding a solution to this.
Im trying to see if an array contains a value that I give
I want all tickets of a specific organization.
The organization is saved in the user (Creator) in an array named organizations.
Models
ticket {
creator: "ObjectId"
}
user {
organizations: ["ObjectId", "ObjectId"]
}
This is what I have now
Ticket.aggregate([
{
"$lookup": {
"from": User.collection.name,
"localField": "creator",
"foreignField": "_id",
"as": "creator"
}
},
{ "$unwind": "$creator" },
{ "$match": { "creator.organizations": {
"$elemMatch": {"$in": ["60a77d5b57d8c960829a0343"]}}}
},
{ "$set": {"creator": "$creator._id"}},
])
This doesn't work tho
I read that you can't use $elemMatch inside an aggregate because its a query. How do I achieve this? I've seen people saying to use a $filter but I have no clue how to make that.
{ $match: { "creator.organizations": {"$in": [ObjectId("60a77d5b57d8c960829a0343")]} } }