本文介绍了使用mongomapper在Array字段中查找包含元素的文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是mongodb/mongomapper的新手,找不到答案.

I am new to mongodb/mongomapper and can't find an answer to this.

我有一个mongomapper类,其中包含以下字段

I have a mongomapper class with the following fields

key :author_id, Integer
key :partecipant_ids, Array

假设我有一个具有以下属性的记录":

Let's say I have a "record" with the following attributes:

{ :author_id => 10, :partecipant_ids => [10,15,201] }

我想检索ID为15的参与者所涉及的所有对象.我在文档中找不到任何提及.

I want to retrieve all the objects where the partecipant with id 15 is involved.I did not find any mention in the documentation.

奇怪的是,以前我在做这个查询

The strange thing is that previously I was doing this query

MessageThread.where :partecipant_ids => [15]

可以,但是在gem/mongodb版本中(也许)进行了一些更改之后,它停止了工作.不幸的是,我不知道以前使用的是哪个版本的mongodb和mongomapper.

which worked, but after (maybe) some change in the gem/mongodb version it stopped working.Unfortunately I don't know which version of mongodb and mongomapper I was using before.

推荐答案

在当前版本的MongoMapper中,这将起作用:

In the current versions of MongoMapper, this will work:

MessageThread.where(:partecipant_ids => 15)

这应该也可以工作...

And this should work as well...

MessageThread.where(:partecipant_ids => [15])

...因为大胆的自动将其扩展为:

...because plucky autoexpands that to:

MessageThread.where(:partecipant_ids => { :$in => [15] })

(请参见 https://github.com/jnunemaker/plucky/blob /master/lib/plucky/criteria_hash.rb#L121 )

我想说一下您的数据,并在Mongo控制台中尝试查询以确保您的查询有效. MongoDB查询直接转换为MM查询,但上述(以及其他一些次要)警告除外.参见 http://www.mongodb.org/display/DOCS/Querying

I'd say take a look at your data and try out queries in the Mongo console to make sure you have a working query. MongoDB queries translate directly to MM queries except for the above (and a few other minor) caveats. See http://www.mongodb.org/display/DOCS/Querying

这篇关于使用mongomapper在Array字段中查找包含元素的文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 20:32