本文介绍了活动记录查询,其中在数组字段值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为统计表在我的MongoDB数据库的Rails 3。

I have a table called " Stat " in my MongoDB database in Rails 3 .

在该表中,有一个名为服务的数组字段。

In that table, there is an array field called "services" .

我希望找到有一个服务数组包含值灯的所有统计。

I want to find all Stats that have a services array that contains the value "lights" .

我想做的事情是这样的:

I want to do something like this :

@stats = Stat.all
@stats1 =  @stats.where("services contains lights")
Rails.logger.info "result:  #{@stats1.count}  " 

我已经试过各种事物,广泛用Google搜索了一下,发现了一些线索,但没有什么似乎工作。我有四个记录,应该匹配这个查询,但上面返回一组零。

I've tried various things and Googled it extensively, found some leads but nothing that seems to work. I have four records that should match this query but the above returns a zero set.

就是我想做的事情有可能在轨道3 /蒙戈?

Is what I want to do possible in rails 3 / mongo ?

推荐答案

好吧,我找到了这个问题的答案:

Ok I found the answer to this question:

@stats = @ stats.where(:services.in => ['灯'])

@stats = @stats.where(:services.in =>['lights'] )

和我也戳周围的倒数发现:

and I also found by poking around that the inverse is :

@stats = @ stats.where(:services.nin => ['灯'])

@stats = @stats.where(:services.nin =>['lights'] )

而不是在

这篇关于活动记录查询,其中在数组字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 00:15