Result in single array of output documents rather than multiple arrays

Can I give result of some mongo join in one array rather than multiple arrays.

Lets say i join

db.A.aggregate([
{$lookup:
{from: "B",
let: { q: "$q", r: "$r" },
pipeline: [
{ $match:
{ $expr:
{$or:[
{ $and:
[
{ $eq: [ "$Type","Abc" ] },
{ $eq: [ "$Number", "$$Number" ] }

]
},
{$and:
    
[   { $eq: [ "$Type","Email" ] },
    { $eq: [ "$Numbers", "$$Numbers" ] }

]
}]
}
}
}], "as": "Result"
}}
]).pretty()

Now it results multiple joined objects like

{
    "_id" : {
        "a" : "2",
    },
  abc:1,
    "qwe" : [ 
        "ert"
    ],
    
    "Result" : [ 
        {
            "_id" : {
               "a" : "2",
            },
            "emailAddresses" : [ 
                "ert"
            ],
            "dfg" : "dsf234"
        }
    ]
},
{
    "_id" : {
        "a" : "3",
    },
  abc:1,
    "qwe" : [ 
        "ert"
    ],
    
    "Result" : [ 
        {
            "_id" : {
               "a" : 2",
            },
            "emailAddresses" : [ 
                "ert"
            ],
            "dfg" : "dsf234"
        }
    ]
}

What I want is all result in single array and no result array like

[{
    "_id" : {
        "a" : "2",
    },
  abc:1,
  "qwe" : [ "ert"],
      [ 
        {
        "_id" : {
        "a" : "2",
            },
        "emailAddresses" : [ "ert" ],
        "dfg" : "dsf234"
        }
    ]
},
{
    "_id" : {
        "a" : "3",
    },
  abc:1,
    "qwe" : [ "ert"],
    
    [ 
        {
            "_id" : {
               "a" : 2",
            },
            "emailAddresses" : [ 
                "ert"
            ],
            "dfg" : "dsf234"
        }
    ]
}
]

How can I achieve this!

Hello @MWD_Wajih_N_A,

the above is the result array from the $lookup stage of your aggregation query. That is what you have defined in the as field ("as": "Result").

From the documentation of $lookup:

as: Specifies the name of the new array field to add to the input documents. The new array field contains the matching documents from the from collection. If the specified name already exists in the input document, the existing field is overwritten .

The remaining part of the output is from the input collection A.

yes yes I know about that. What I am really trying to do is
Bring all resultant documents in one document
and
bring the resultant object to the same level as non resultant objects by removing the “result” (as) keyword.

To achieve that you need to use projection using $addFields or $project stage. You can include an example output document of what you are visualizing.

By doing that, I still get multiple different documents in output. I want to make sure that the resultant output is one document containing all the resultant documents at the same level.

Generally, resultant output is like
{},{},{},{}
I want all these 4 in one document like
{
{},{},{},{}
}

@MWD_Wajih_N_A You can do it by adding a $group with push:

{
// The _id of a group should be the identifying grouping expression
$group : { $id : { ... } ,
"Results" : {$push : "$Result"}
}
}

Let me know if this helps.

I want to add my 2 cents.

Sometimes we forget that the result of a pipeline is simply data and some front end will present this information with some decorations. Fighting at the data layer to have a very specific document format is sometime overkill. At other times having a field named Result in the application conveys more information as we know it is the result of an aggregation or computation. With all the fields at the same level, we lose that information.

Just my 2 cents. Some situations do require specific fields at a specific positions.

1 Like

Yeah right. That’s a specific use case