I have documents mongoDB in behind.
我后面有mongoDB文件。
{ "_id" : ObjectId("5ac06b6fb49124f4e5602817"), "status" : -1, "createdAt" : ISODate("2018-03-31T05:17:35.557Z") }
{“_ id”:ObjectId(“5ac06b6fb49124f4e5602817”),“status”: - 1,“createdAt”:ISODate(“2018-03-31T05:17:35.557Z”)}
{ "_id" : ObjectId("5ac0b51cb08b6c0014d1d0c1"), "status" : 0, "createdAt" : ISODate("2018-04-01T10:31:56.199Z") }
{“_ id”:ObjectId(“5ac0b51cb08b6c0014d1d0c1”),“status”:0,“createdAt”:ISODate(“2018-04-01T10:31:56.199Z”)}
{ "_id" : ObjectId("5ac0b538b08b6c0014d1d0c2"), "status" : 2, "createdAt" : ISODate("2018-04-01T10:32:24.542Z") }
{“_ id”:ObjectId(“5ac0b538b08b6c0014d1d0c2”),“status”:2,“createdAt”:ISODate(“2018-04-01T10:32:24.542Z”)}
i used $group, $match, $sum and i only can return: { _id: { month: 4, day: 1, year: 2018 }, count: 2 }
我使用$ group,$ match,$ sum我只能返回:{_ id:{month:4,day:1,year:2018},count:2}
i want return: { _id: { month: 4, day: 1, year: 2018 }, count : 2, countStatus0 : 1, countStatus2 : 1 }
我想要返回:{_ id:{month:4,day:1,year:2018},count:2,countStatus0:1,countStatus2:1}
i wish you help me... Thank you so much. Sorry my English is bad.
我希望你帮助我......非常感谢你。对不起,我的英语不好。
1 个解决方案
#1
0
You can try below aggregation:
您可以尝试以下聚合:
db.col.aggregate([
{
$group: {
_id: {
year: { $year: "$createdAt" },
month: { $month: "$createdAt" },
day: { $dayOfMonth: "$createdAt" },
status: "$status"
},
count: { $sum: 1 }
}
},
{
$group: {
_id: {
year: "$_id.year",
month: "$_id.month",
day: "$_id.day"
},
count: { $sum: "$count" },
statuses: { $push: { k: { $concat: ["status", {$substr:["$_id.status", 0, -1 ]}] }, v: "$count" } }
}
},
{
$project: {
_id: 1,
count: 1,
statuses: { $arrayToObject: "$statuses" }
}
},
{
$replaceRoot: {
newRoot: {
$mergeObjects: [ "$$ROOT", "$statuses" ]
}
}
},
{
$project: {
statuses: 0
}
}
])
To get total sum and sums for each status you have to use $group
twice. Each partial status can be converted to a pair of k
and v
where key
is a dynamically generated string which is a concatenation of status
and its value like status0
, status2
. Having those pairs you can use $arrayToObject to generate object fields from that array. Then you can use $mergeObjects with $replaceRoot to merge root with nested statuses
object.
要获得每个状态的总和和总和,您必须使用$ group两次。每个部分状态可以转换为一对k和v,其中key是动态生成的字符串,它是状态及其值的串联,如status0,status2。拥有这些对,您可以使用$ arrayToObject从该数组生成对象字段。然后,您可以使用$ mergeObjects和$ replaceRoot将root与嵌套状态对象合并。
There is one trick here. Since MongoDB does not allow to concatenate string with double we have to use $substr
on double to convert it to a string.
这里有一个技巧。由于MongoDB不允许将字符串与double连接,我们必须在double上使用$ substr将其转换为字符串。
#1
0
You can try below aggregation:
您可以尝试以下聚合:
db.col.aggregate([
{
$group: {
_id: {
year: { $year: "$createdAt" },
month: { $month: "$createdAt" },
day: { $dayOfMonth: "$createdAt" },
status: "$status"
},
count: { $sum: 1 }
}
},
{
$group: {
_id: {
year: "$_id.year",
month: "$_id.month",
day: "$_id.day"
},
count: { $sum: "$count" },
statuses: { $push: { k: { $concat: ["status", {$substr:["$_id.status", 0, -1 ]}] }, v: "$count" } }
}
},
{
$project: {
_id: 1,
count: 1,
statuses: { $arrayToObject: "$statuses" }
}
},
{
$replaceRoot: {
newRoot: {
$mergeObjects: [ "$$ROOT", "$statuses" ]
}
}
},
{
$project: {
statuses: 0
}
}
])
To get total sum and sums for each status you have to use $group
twice. Each partial status can be converted to a pair of k
and v
where key
is a dynamically generated string which is a concatenation of status
and its value like status0
, status2
. Having those pairs you can use $arrayToObject to generate object fields from that array. Then you can use $mergeObjects with $replaceRoot to merge root with nested statuses
object.
要获得每个状态的总和和总和,您必须使用$ group两次。每个部分状态可以转换为一对k和v,其中key是动态生成的字符串,它是状态及其值的串联,如status0,status2。拥有这些对,您可以使用$ arrayToObject从该数组生成对象字段。然后,您可以使用$ mergeObjects和$ replaceRoot将root与嵌套状态对象合并。
There is one trick here. Since MongoDB does not allow to concatenate string with double we have to use $substr
on double to convert it to a string.
这里有一个技巧。由于MongoDB不允许将字符串与double连接,我们必须在double上使用$ substr将其转换为字符串。