Hi ,
I have one problem while writing query. I want to perform lookup operation on two collection.
There are two collection user and customer.
user col:-
_id: ObejectId ("2325abcdef"),
description: Rahul Upraity,
address: xvyz
.
.
customer col :-
_id: ObjectId
name: abcd
type: 454
userLink :[
userId : ObjectId ("") - foriegn field
startDate: Date
endDate: Date
]
So Customer has userLinks and contains multiple users and endDate in user Link is expiration of user with that Customer.
I want to aggregate on user collection as I have to search on description field and I want to look up with customer and to get all customers connected with that user and one more condition we have to consider is that endDate should not be there for that user.
I am able to join but not able incorporate the second condition i.e. endDate should not exist for that user.
Solution I tried:-
[
{
$match:
/**
* query: The query in MQL.
*/
{
"user.description": "XYZ",
},
},
{
$lookup:
/**
* from: The target collection.
* localField: The local join field.
* foreignField: The target join field.
* as: The name for the results.
* pipeline: Optional pipeline to run on the foreign collection.
* let: Optional variables to use in the pipeline field stages.
*/
{
from: "customer",
let: {
user_id: "$_id",
},
pipeline: [
{
$match: {
$expr: {
$eq: [
"$$user_id",
"$userLink.userId",
],
},
},
},
],
as: "customers",
},
},