我有以下聚合查询
db.orders.aggregate(
{ "$project" :
{ _id : 0, "dateDay" :
{"$concat" : [
{"$substr" : [{"$dayOfMonth" : "$timestamp"}, 0, 2]}, "-",
{"$substr" : [{"$month" : "$timestamp"}, 0, 2]}, "-",
{"$substr" : [{"$year" : "$timestamp"}, 0, 4]}
] }
}
},
{ "$group" :
{ "_id" : "$dateDay", "Count" : { "$sum" : 1 } }
},
{ $sort : { "Count : 1 } }
);
返回类似的东西:
{
"_id" : "3-9-2014",
"Count" : 81
},
{
"_id" : "14-12-2014",
"Count" : 10
},
{
"_id" : "2-9-2014",
"Count" : 98
},
{
"_id" : "1-9-2014",
"Count" : 118
},
{
"_id" : "20-8-2014",
"Count" : 79
},
{
"_id" : "8-6-2015",
"Count" : 128
}
],
"ok" : 1
}
如何按日-月-年的顺序对结果进行排序?
{ $sort : { "Count : 1 } } - 给我按计数排序,但我喜欢像 MySQL 查询这样的东西:
mysql> SELECT DATE(stamp), DAYNAME(stamp), COUNT(*) FROM user_orders WHERE DATE(stamp) >= DATE('2015-05-29') AND DATE(stamp) <= DATE('2015-06-19') GROUP BY DATE(stamp);
+-------------+----------------+----------+
| DATE(stamp) | DAYNAME(stamp) | COUNT(*) |
+-------------+----------------+----------+
| 2015-05-29 | Friday | 63 |
| 2015-05-30 | Saturday | 9 |
| 2015-05-31 | Sunday | 11 |
| 2015-06-01 | Monday | 94 |
| 2015-06-02 | Tuesday | 92 |
| 2015-06-03 | Wednesday | 109 |
| 2015-06-04 | Thursday | 89 |
| 2015-06-05 | Friday | 68 |
| 2015-06-06 | Saturday | 9 |
| 2015-06-07 | Sunday | 11 |
| 2015-06-08 | Monday | 125 |
| 2015-06-09 | Tuesday | 101 |
| 2015-06-10 | Wednesday | 97 |
| 2015-06-11 | Thursday | 110 |
| 2015-06-12 | Friday | 85 |
| 2015-06-13 | Saturday | 13 |
| 2015-06-14 | Sunday | 6 |
| 2015-06-15 | Monday | 113 |
| 2015-06-16 | Tuesday | 109 |
| 2015-06-17 | Wednesday | 125 |
| 2015-06-18 | Thursday | 77 |
| 2015-06-19 | Friday | 81 |
+-------------+----------------+----------+
22 rows in set (0.05 sec)
非常感谢任何建议
最佳答案
不是将第一个管道阶段作为 $project 运算符步骤,您可以将 $group 将日期聚合运算符作为组键表达式的管道步骤。以下 $sort 运算符管道将按照日-月-年的顺序对这些键进行排序,如本例所示:
db.orders.aggregate([
{
"$group": {
"_id": {
"day": { "$dayOfMonth" : "$timestamp" },
"month": { "$month" : "$timestamp" },
"year": { "$year" : "$timestamp" }
},
"Count": { "$sum" : 1 }
}
},
{
"$sort": {
"_id.year": 1,
"_id.month": 1,
"_id.day": 1
}
},
{
"$project": {
"_id": 0,
"dateDay": {
"$concat": [
{"$substr" : [ "$_id.day", 0, 2]}, "-",
{"$substr" : [ "$_id.month", 0, 2]}, "-",
{"$substr" : [ "$_id.year", 0, 4]}
]
},
"Count": 1
}
}/*,
{ $sort : { "Count : 1 } }
*/
]);
关于mongodb 聚合查询排序结果并显示日期名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31103624/
我正在用Ruby编写一个简单的程序来检查域列表是否被占用。基本上它循环遍历列表,并使用以下函数进行检查。require'rubygems'require'whois'defcheck_domain(domain)c=Whois::Client.newc.query("google.com").available?end程序不断出错(即使我在google.com中进行硬编码),并打印以下消息。鉴于该程序非常简单,我已经没有什么想法了-有什么建议吗?/Library/Ruby/Gems/1.8/gems/whois-2.0.2/lib/whois/server/adapters/base.
我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
所以我在关注Railscast,我注意到在html.erb文件中,ruby代码有一个微弱的背景高亮效果,以区别于其他代码HTML文档。我知道Ryan使用TextMate。我正在使用SublimeText3。我怎样才能达到同样的效果?谢谢! 最佳答案 为SublimeText安装ERB包。假设您安装了SublimeText包管理器*,只需点击cmd+shift+P即可获得命令菜单,然后键入installpackage并选择PackageControl:InstallPackage获取包管理器菜单。在该菜单中,键入ERB并在看到包时选择
我想设置一个默认日期,例如实际日期,我该如何设置?还有如何在组合框中设置默认值顺便问一下,date_field_tag和date_field之间有什么区别? 最佳答案 试试这个:将默认日期作为第二个参数传递。youcorrectlysetthedefaultvalueofcomboboxasshowninyourquestion. 关于ruby-on-rails-date_field_tag,如何设置默认日期?[rails上的ruby],我们在StackOverflow上找到一个类似的问
我试图在索引页中创建一个超链接,但它没有显示,也没有给出任何错误。这是我的index.html.erb代码。ListingarticlesTitleTextssss我检查了我的路线,我认为它们也没有问题。PrefixVerbURIPatternController#Actionwelcome_indexGET/welcome/index(.:format)welcome#indexarticlesGET/articles(.:format)articles#indexPOST/articles(.:format)articles#createnew_articleGET/article
我知道我可以指定某些字段来使用pluck查询数据库。ids=Item.where('due_at但是我想知道,是否有一种方法可以指定我想避免从数据库查询的某些字段。某种反拔?posts=Post.where(published:true).do_not_lookup(:enormous_field) 最佳答案 Model#attribute_names应该返回列/属性数组。您可以排除其中一些并传递给pluck或select方法。像这样:posts=Post.where(published:true).select(Post.attr
我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c
我需要检查DateTime是否采用有效的ISO8601格式。喜欢:#iso8601?我检查了ruby是否有特定方法,但没有找到。目前我正在使用date.iso8601==date来检查这个。有什么好的方法吗?编辑解释我的环境,并改变问题的范围。因此,我的项目将使用jsapiFullCalendar,这就是我需要iso8601字符串格式的原因。我想知道更好或正确的方法是什么,以正确的格式将日期保存在数据库中,或者让ActiveRecord完成它们的工作并在我需要时间信息时对其进行操作。 最佳答案 我不太明白你的问题。我假设您想检查
我的日期格式如下:"%d-%m-%Y"(例如,今天的日期为07-09-2015),我想看看是不是在过去的七天内。谁能推荐一种方法? 最佳答案 你可以这样做:require"date"Date.today-7 关于ruby-检查日期是否在过去7天内,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/32438063/