I created an aggregate query that uses the 'parts' key as the '_id' and returns all the 'subparts' numbers associated in an array. The output from the aggregate query is undesirable as I get the array 'result' which contains 74755 dictionary objects. Each object contains an int 32 for the _id and an array for '#associated_subparts'. I can access sub-elements via:
db.agg_result.find({}, {_id: 0, result: { $slice: [0,2]}})
but this is just a positional method. I tried variants of nested $elemMatch and $all queries with no success. I want to be able to return the object with id 468183339 for example. I might be projecting my original aggregate query incorrectly, but I'd like to return a collection without the nest so each object in the collection is one of these parts > subparts objects.
Example Query:
var agg_out = db.collection.aggregate( { $group :{ '_id' : "$parts#",
'associated_subparts#' : { $addToSet : "$sub_part" }}});
db.agg_result.insert(agg_out);
db.agg_result.find();
Example Output:
{
"_id" : ObjectId("52b4c4c6e984c01d69ff176f"),
"result" : [
{
"_id" : 468183339,
"associated_subparts#" : [
-1408237536
]
},
{
"_id" : 782155933,
"associated_subparts#" : [
-1408237536
]
},
{
"_id" : 1583659973,
"associated_subparts#" : [
-1408237535,
-1408237535,
-1408237535,
-1408237535,
-1408237535,
-1408237535,
-1408237535,
-1408237535,
-1408237535,
-1408237535,
-1408237535,
-1408237535
]
}
]
}