Hello I'm having a hard time getting this complex return using MongoDB nor Javascript. Hope can anyone teach me how to get this return.
Admin.
group result by id
user
flatten result
Here's the data example.
let user = [
{
_id: 123,
name: 'John',
createdAt: "2015-08-12T00:00:00Z"
},
{
_id: 124,
name: 'Jane',
createdAt: "2015-09-12T00:00:00Z"
},
{
_id: 125,
name: 'Robert',
createdAt: "2015-09-12T00:00:00Z"
},
{
_id: 126,
name: 'Samson',
createdAt: "2016-11-12T00:00:00Z"
}
]
Expected Result
user
[
{
"15-8": 1 //yyyy-mm: number of data for the month of august
},
{
"15-9": 2
},
{
"16-11": 1
}
]
admin
[
{
"15-8":
{
_id: 123,
count: 1
}
},
{
"15-9": {
_id: 124,
count: 1,
},{
_id: 125,
count: 1
},
{
"16-11": {
_id: 126,
count: 1
}
}
]
question from:
https://stackoverflow.com/questions/65947873/group-result-using-date-from-object-keys-and-only-get-count-the-results 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…