[问题]
I have a collection called Document in MongoDB. Documents in this collection have a field called CreationDate stored in ISO date type. My task is to count the number of documents created per day and sort by the number asynchronously. The output format is required to be [{_id:'yyyy-MM-dd', cnt:x}]. I tried to use aggregation framework as below. db.Document.aggregate( , {$project: {_id:1, Year:{$year:'$CreationDate'}, Month:{$month:'$CreationDate'}, Date:{$dayOfMonth:'$CreationDate'}}} , {$group: {_id:{$concat:['$Year', '-', '$Month', '-', '$Date']}, cnt:{$sum:1}}} , {$sort:{'cnt':-1}} ); The code gives me error as below: $concat only supports strings, not NumberInt32 I understand this is because $year, $month and $dayOfMonth all return number. It's possible to compose the _id field as an object and re-format it in the desired format in application level. But from technical perspective, I have two questions:
Thanks in advance for any advice.
|
[回答]
You can do this with db.Document.aggregate([ { "$group": { "_id":{ "$concat": [ { "$substr": [ { "$year": "$CreationDate" }, 0, 4 ] }, "-", { "$cond": [ { "$gt": [ { "$month": "$CreationDate" }, 9 ] }, { "$substr": [ { "$month": "$CreationDate" }, 0, 2 ] }, { "$concat": [ "0", { "$substr": [ { "$month": "$CreationDate" }, 0, 1 ] }, ]}, ]}, "-", { "$cond": [ { "$gt": [ { "$dayOfMonth": "$CreationDate" }, 9 ] }, { "$substr": [ { "$dayOfMonth": "$CreationDate" }, 0, 2 ] }, { "$concat": [ "0", { "$substr": [ { "$dayOfMonth": "$CreationDate" }, 0, 1 ] }, ]} ]} ] }, { "cnt": { "$sum": 1 } } }} { "$sort":{ "cnt" :-1 }} ]); Possibly better is to just use date math instead, this returns an epoch timestamp value, but it is easy to work into a date object in post processing: db.Document.aggregate([ { "$group": { "_id": { "$subtract": [ { "$subtract": [ "$CreationDate", new Date("1970-01-01") ] }, { "$mod": [ { "$subtract": [ "$CreationDate", new Date("1970-01-01") ] }, 1000 * 60 * 60 * 24 ]} ] }, "cnt": { "$sum": 1 } }}, { "$sort": { "cnt": -1 } } ])
|
来自: https://stackoverflow.com/questions/25176855/data-type-conversion-in-mongodb