与Mongoid聚集 [英] Aggregate with Mongoid

查看:60
本文介绍了与Mongoid聚集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MongoDB有一个新的聚合框架,我正在尝试弄清楚如何将其与Mongoid一起使用.似乎在此处中讨论过,Moped的一个分支具有此功能.我已经更新到MongoDB 2.2,并尝试像这样在我的应用程序上安装Moped的这个分支:

MongoDB has a new Aggregation Framework and I'm trying to figure out how to use it with Mongoid. It appears there is a branch of Moped with this functionality as discussed here. I've updated to MongoDB 2.2 and tried installing this branch of Moped on my app like this:

gem'moped',git:'git://github.com/mongoid/moped.git',分支: 聚合支持"

gem 'moped', git: 'git://github.com/mongoid/moped.git', branch: 'aggregation-support'

,但聚合仍无法正常进行.这是我用来测试的电话:

but aggregation is still not working. This is the call I am using to test it:

= Post.all.aggregate({"$ group" => {"_id" =>"$ _id"}})

= Post.all.aggregate({ "$group" => { "_id" => "$_id" } })

更新

在mongo shell中,它起作用:

In the mongo shell this works:

db.users.aggregate({$ group:{_id:"$ _id"}})

db.users.aggregate({ $group : { _id : "$_id" }})

所以我认为这是一个Mongoid问题...任何单词都很棒!

so I'm thinking it's a Mongoid issue...any word on this would be great!

推荐答案

从3.0.1开始,Mongoid需要MongoDB 2.2并支持新的聚合框架.

Since 3.0.1, Mongoid requires MongoDB 2.2 and supports the new aggregation framework.

您现在可以在集合上调用aggregate:

You can now call aggregate on collections:

project = {"$project" => 
  {
    "charges" => 1,
    "year"    => { "$year" => "$created"}, "month" => { "$month" => "$created"},
    "week"    => { "$week" => "$created"}, "day"   => { "$dayOfYear" => "$created"} 
  }
}
group =  { "$group" =>
  { "_id" => {"year"=>"$year", "week"=>"$week"}, "count" => { "$sum" => 1 } } 
}
Charge.collection.aggregate([project,group])

这篇关于与Mongoid聚集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆