I needed to join two collection which I joined successfully. Since the joined collection documents are shown as sub document array in the output of lookup.
Now, what I want is to bring all he documents on the same root level
My successful query is
db.activities.aggregate([
{ $match : { userId : "123" } },
{ $lookup: {
from: "activities_m",
let: { emailAddresses: "$emailAddresses", phoneNumbers: "$phoneNumbers" },
pipeline: [
{ $match:
{ $expr:
{ $or: [
{ $and: [
{ $eq: [ "$activityType","SMS" ] },
{ $eq: [ "$phoneNumbers", "$$phoneNumbers" ] }
]},
{ $and: [
{ $eq: [ "$activityType","Email" ] },
{ $eq: [ "$emailAddresses", "$$emailAddresses" ] }
]}
]}
}
}
],
"as": "Result"
}}
]).pretty()
and the output result is
[
/* 1 */
{
"_id" : {...
},
"activityId" : 9996666,
"activityType" : "Email",
"duration" : 2,
"emailAddresses" : [
"ewqqw@yu.com"
],
"endTime" : "2021-01-13T09:49:15.783+04:00",
"intent" : null,
"owner" : "ewqqw@yu.com",
"readReceipt" : "",
"sentimentAnalysis" : "",
"tokenIds" : null,
"userId" : "66666",
"Result" : [
{
"_id" : {... },
"activityId" : 999,
"activityType" : "Email",
"duration" : 2,
"emailAddresses" : [
"ewqqw@yu.com"
],
"intent" : null,
"owner" : "ewqqw@yu.com",
"readReceipt" : "",
"sentimentAnalysis" : "",
"tokenIds" : null,
"userId" : "66666"
}
]
}
/* 2 */
{
"_id" : {... },
"activityId" : 997,
"activityType" : "SMS",
"duration" : 0,
"intent" : null,
"owner" : "wer",
"phoneNumbers" : "886666",
"readReceipt" : "",
"sentimentAnalysis" : "",
"title" : "",
"tokenIds" : null,
"userId" : "66666",
"Result" : [
{
"_id" : {...
},
"activityId" : 9976,
"activityType" : "SMS",
"duration" : 0,
"intent" : null,
"owner" : "wer",
"phoneNumbers" : "886666",
"readReceipt" : "",
"sentimentAnalysis" : "",
"title" : "",
"tokenIds" : null,
"userId" : "66666"
}
]
}
Now I want
Resultant array at root level as object
all documents in one big document
Like
[
/* 1 */
{
"activityId" : 9996666,
"activityType" : "Email",
"deliveryReport" : "",
"duration" : 2,
"emailAddresses" : [
"ewqqw@yu.com"
],
"sentimentAnalysis" : "",
"tokenIds" : null,
"userId" : "66666",
}, {
"activityId" : 999,
"activityType" : "Email",
"duration" : 2,
"emailAddresses" : [
"ewqqw@yu.com"
],
"intent" : null,
"sentimentAnalysis" : "",
"tokenIds" : null,
"userId" : "66666"
}
}
/* 2 */
{
"activityId" : 997,
"activityType" : "SMS",
"duration" : 0,
"intent" : null,
"phoneNumbers" : "886666",
"sentimentAnalysis" : "",
"tokenIds" : null,
"userId" : "66666",
}, {
"activityId" : 9976,
"activityType" : "SMS",
"duration" : 0,
"phoneNumbers" : "886666",
"sentimentAnalysis" : "",
"tokenIds" : null,
"userId" : "66666"
}
}
]
How can I possibly achieve this