Mongoid`group()`条件 [英] Mongoid `group()` conditions

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

问题描述

我想为我在Mongoid中的分组提供条件,但是如何在条件哈希中为属性发送多个值?这就是我想做的:

I want to provide a condition for my grouping in Mongoid, but how can I send in multiple values for an attribute in the conditions hash? This is what I want to do:

PageViews.collection.group(
  cond: {page_id: ['4e6912618083ab383e000010', '4e6912618083ab383e000009']},
  key: 'timestamp',
  initial: {count: 0},
  reduce: "function(x, y) {y.count += x.count;}"
)

因此,带有page_id的任何PageView将不在查询范围之内,但我似乎根本无法使条件哈希(cond)正常工作!我在这里做错了,我真的很困惑.

So therefore any PageView with the either page_id will be apart of the query, but I can't seem to get the condition hash (cond) to work at all! What am I doing wrong here, I'm really confused.

以下是group()方法的API文档: http://api.mongodb.org/ruby/current/Mongo/Collection.html#group-instance_method

Here is the API docs for the group() method: http://api.mongodb.org/ruby/current/Mongo/Collection.html#group-instance_method

任何帮助将不胜感激.

更新

以这种方式运行查询:

PageViews.collection.group(
  cond: {page_id: { $in : ['4e6912618083ab383e000010', '4e6912618083ab383e000009']}},
  key: 'timestamp',
  initial: {count: 0},
  reduce: "function(x, y) {y.count += x.count;}"
)

返回以下错误,但是语法对我来说很好:

Returns the following error, but the syntax looks fine to me:

SyntaxError: (irb):170: syntax error, unexpected ':', expecting tASSOC
  cond: {page_id: { $in : ['4e6912618083ab383e000010',...
                         ^
(irb):170: syntax error, unexpected '}', expecting $end
...', '4e6912618083ab383e000009']}},
...                               ^
    from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.1.0/lib/rails/commands/console.rb:45:in `start'
    from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.1.0/lib/rails/commands/console.rb:8:in `start'
    from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.1.0/lib/rails/commands.rb:40:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

令人沮丧的是,到处都没有这样的示例,文档说条件参数就是这样:

It's frustrating that there are like no examples of this anywhere, the docs say that the condition parameter is as such:

(String,BSON :: Code):cond —默认值:{} —指定一个 查询以筛选在其上运行聚合的文档 (可选).

(String, BSON::Code) :cond — default: {} — A document specifying a query for filtering the documents over which the aggregation is run (optional).

那么,如果要使用StringBSON::Code,它会是什么样?

So if it takes a String or BSON::Code, what would this look like?

推荐答案

您需要对查询使用来匹配多个值.因此它将写为

You need to use $in for the query to match against multiple values. So it would be written as

PageViews.collection.group(
  :cond => {:page_id => { '$in' => ['4e6912618083ab383e000010', '4e6912618083ab383e000009']}},
  :key => 'timestamp',
  :initial => {count: 0},
  :reduce => "function(x, y) {y.count += x.count;}"
)    

您可以在此处了解更多信息: http://www .mongodb.org/display/DOCS/Advanced + Queries#AdvancedQueries-%24in

You can read more about it here: http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%24in

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

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