Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-751

Query array in embedded documents using $elemMatch does not work

    • Type: Icon: Task Task
    • Resolution: Done
    • 12_01_17
    • Affects Version/s: None
    • Component/s: None
    • Labels:

      Hi,

      I have the following documents:
      <p>class User </p>
      <p> include Mongoid::Document</p>
      <p> embeds_many :profiles</p>
      <p>end </p>
      </p>

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

      Querying the array field "data" inside the Profile document
      but it does not work. It always get zero hits.

      Here is the code:
      <p>
      user_profile = UserProfile.new </p>
      <p>user_profile.data = [

      {"action" => "del", "approve" => "always"}

      ,

      {"action" => "add", "approve" => "never"}

      ] </p>

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

      <p>user = User.find(:first, :conditions =>

      {"name" => "hugo"}

      ) </p>
      <p>user.profiles.where("data" => { "$in" => { "$elemMatch" => { "action"
      => "add" }} }).size()
      </p>

            Assignee:
            Unassigned Unassigned
            Reporter:
            Migrate Migrate [X]
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: