How to filter the lookup results of aggregation in MongoDB?

0 like 0 dislike
29 views
Good time of day. There is the following task.
Is the substance of the Event, each having its own description, which has their list of organizers in an array of references and the list of workshops (essence Workshop).
const EventSchema = new Schema( { title: {type: String, default:", required: true}, description: {type: String, default:"}, startDate: {type: Date, default: Date.now, required: true}, founders: [{ type: Schema.Types.ObjectId, ref: 'User' }], workshops: [{ type: Schema.Types.ObjectId, ref: 'Workshop' }], });

Two entities are stored in separate collections.
Sample structure of the entity Workshop
const WorkshopSchema = new Schema( { title: {type: String, default:", required: true}, description: {type: String, default:"}, event:{ type: Schema.Types.ObjectId, ref: 'Event' }, actors: [{ type: Schema.Types.ObjectId, ref: 'User' }], });


One event(of the event) may have a number of workshops(master classes).

To display events on the website of the workshop is populate the request field workshops,
the essence of the workshop is also a field Event, which is populate to display the parameters of the Event visit the specific workshop.

What's the problem - you need to pull out all the workshops related to a specific user, namely: it can be among the actor s specific workshop or in the list of founder s Event(then everything associated with the event workshops need to pull)

There is an assumption that you need to make the right aggregation: originally pigeonite lookupом workshops to their parent event, and then to filter based on the OR condition (the presence of the id of the user Event.founders and Workshop.actors)

Directly lookup looks like this
db.getCollection('workshops').aggregate([ { $lookup: { from: "events', localField: "event", foreignField: "_id", as: "events" } } ])


Please tell me how to do subsequent OR filter by Event.founders and Workshop.actors for a specific user ID(presence ID array) or how else to build the query logic.

Thanks in advance.
by | 29 views

1 Answer

0 like 0 dislike
Add a $match after $lookup using the $operator or. You can access the fields.

Something like this:
$lookup..., $or: [ {actors: USER_OBJECT}, {'events.founders': USER_OBJECT} ]
by

Related questions

0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
asked May 22, 2019 by tgpglstbc
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
asked Apr 1, 2019 by NiceForce
110,608 questions
257,186 answers
0 comments
28,662 users