Query array in embedded documents using $elemMatch

518 views
Skip to first unread message

Hugo

unread,
Mar 9, 2011, 3:51:07 PM3/9/11
to Mongoid
Hi all,

I have the following documents:

class User
include Mongoid::Document
embeds_many :profiles
end

class Profile
include Mongoid::Document
field :data, :type => Array, :default => []
embedded_in :user, :inverse_of => :profiles
end

I'm trying to query the array field "data" inside the Profile document
but it does not work. I always get zero hits.
Here is the code:

user_profile = UserProfile.new
user_profile.data = [{"action" => "del", "approve" => "always"},
{"action" => "add", "approve" => "never"}]

user = User.new
user.name = "hugo"
user.profiles << user_profile
user.save

user = User.find(:first, :conditions => {"name" => "hugo"})
user.profiles.where("data" => { "$in" => { "$elemMatch" => { "action"
=> "add" }} }).size()

Can anyone help me?

Thanks.
Best regards,
Hugo

Durran Jordan

unread,
Mar 10, 2011, 2:24:15 AM3/10/11
to mon...@googlegroups.com
You caught me on this one - I need to implement an $elemMatch matcher for embedded documents in Mongoid. Can you open a Github issue for me please - it should be a quick one to implement.

2011/3/9 Hugo <hugo...@gmail.com>

Hugo

unread,
Mar 10, 2011, 5:20:17 AM3/10/11
to Mongoid
Hi,

Thanks for the answer.

I've created the following issue:
https://github.com/mongoid/mongoid/issues/750

Best regards,
Hugo

On 10 Mar, 07:24, Durran Jordan <dur...@gmail.com> wrote:
> You caught me on this one - I need to implement an $elemMatch matcher for
> embedded documents in Mongoid. Can you open a Github issue for me please -
> it should be a quick one to implement.
>
> 2011/3/9 Hugo <hugo....@gmail.com>
Reply all
Reply to author
Forward
0 new messages