Let us say I have a collection of users. Each user has assets. There are some statistical measures that I want to perform on assets. But in order to be able to do this I need to only have the assets in the projection as an input for the aggregate. How could this be achieved?
I want to be able to do a bunch of aggregate operations on assets. These operations would include filtering, limit and skip so I would be using operators like $skip, $match, $limit, and others. But in order for me to perform these operations on the embedded array, I would need an operator that takes a user document and outputs the assets array embedded into it.
Simply I want an operator that that takes a user document with the following schema and outputs an array of assets that I further perform aggregate operations on it.
user:{
name: string,
assets: Asset[],
age: number
}
I tried this aggregate operations but it is not working as I expected.
db.getCollection('users').aggregate( [
{ $match: {_id: ObjectId("5ae837dca9a8e04dd0689824")} } ,
{ $project : { assets: 1 } },
] )
It outputs an array that have one user document in which the assets are embedded inside like this
[{
"_id" : ObjectId("5ae837dca9a8e04dd0689824"),
"assets" : [{_id:ObjectId("1"), assetName: 'tt' }, {_id : ObjectId("2"), assetName: 'rr'}]
}]
I need to have the output like the one below
[
{_id:ObjectId("1"), assetName: 'tt'},
{_id:ObjectId("2"), assetName: 'rr'}
]