I am trying to create a new question (report) using below criteria for Mongodb but after entering below native query it’s giving me no results after trying multiple things:
db.getCollection(“opportunity”).aggregate(
[
{
“$project” : {
“_id” : NumberInt(0),
“opportunity” : “$$ROOT”
}
},
{
“$lookup” : {
“localField” : “opportunity.stage”,
“from” : “master_list”,
“foreignField” : “_id”,
“as” : “master_list”
}
},
{
“$unwind” : {
“path” : “$master_list”,
“preserveNullAndEmptyArrays” : false
}
},
{
“$match” : {
“master_list.type” : “opp_stage”,
“opportunity.status” : “open”
}
},
{
“$group” : {
“_id” : {
“opportunity᎐stage” : “$opportunity.stage”,
“master_list᎐text” : “$master_list.text”
},
“COUNT(opportunity᎐stage)” : {
“$sum” : NumberInt(1)
}
}
},
{
“$project” : {
“COUNT(opportunity᎐stage)” : “$COUNT(opportunity᎐stage)”,
“master_list.text” : “$_id.master_list᎐text”,
“_id” : NumberInt(0)
}
}
],
{
“allowDiskUse” : true
}
);
The above “Query code” is equivalent to below “SQL Query”
select count ( opportunity.stage ), master_list.text from opportunity inner join master_list
on opportunity.stage = master_list._id
where master_list.type = “opp_stage”
and opportunity.status = “open”
group by opportunity.stage , master_list.text