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

Document recursively_embeds_many filtering with elem_match

    • Type: Icon: Improvement Improvement
    • Resolution: Fixed
    • Priority: Icon: Minor - P4 Minor - P4
    • 7.1.0.rc0
    • Affects Version/s: None
    • Component/s: Docs
    • Environment:
      test

      Hi Team,

      pre-context: I'm pretty new using mongo and I'm dealing with some recursively embeds many structure for creating a tree of folders that I need to handle, and the problem is that I can't figure out a way to get a particular folder from that tree.

      This is my version of mongo from my Gemfile.lock

       

      mongo (2.6.2) 
       bson (>= 4.3.0, < 5.0.0) 
      mongoid (7.0.1) 
       activemodel (>= 5.1, < 6.0.0) 
       mongo (>= 2.5.1, < 3.0.0)
      

       

       

      The Model I did is:

      class Folder
      include Mongoid::Document
       
      field :name, type: String
      field :permissions, type: String
       
      recursively_embeds_many
       
      end

      I started with a simple 1 level structure

      /: (root_folder)
      --> 'ex' (child_folder)
      --> 'aut' (child_folder)
      --> 'dolorem' (child_folder)
      --> 'et' (child_folder)
      --> 'etc' (child_folder)
      2.5.1 :031 > root.child_folders.count
       => 5
      2.5.1 :030 > root.child_folders
       => [#<Folder _id: 5bb52d1b63623919f778f8b1, created_at: 2018-10-03 20:56:59 UTC, updated_at: 2018-10-03 20:56:59 UTC, name: "ex", path: "/accounts/5bb52cc863623919f778f8aa/sapiente/ex", permissions: "ro", item_count: 0, account_id: BSON::ObjectId('5bb52cc863623919f778f8aa')>, #<Folder _id: 5bb52d1b63623919f778f8b2, created_at: 2018-10-03 20:56:59 UTC, updated_at: 2018-10-03 20:56:59 UTC, name: "aut", path: "/accounts/5bb52cc863623919f778f8aa/sapiente/aut", permissions: "rw", item_count: 0, account_id: BSON::ObjectId('5bb52cc863623919f778f8aa')>, #<Folder _id: 5bb52d1b63623919f778f8b3, created_at: 2018-10-03 20:56:59 UTC, updated_at: 2018-10-03 20:56:59 UTC, name: "dolorem", path: "/accounts/5bb52cc863623919f778f8aa/sapiente/dolorem", permissions: "ro", item_count: 0, account_id: BSON::ObjectId('5bb52cc863623919f778f8aa')>, #<Folder _id: 5bb52d1b63623919f778f8b4, created_at: 2018-10-03 20:56:59 UTC, updated_at: 2018-10-03 20:56:59 UTC, name: "et", path: "/accounts/5bb52cc863623919f778f8aa/sapiente/et", permissions: "rw", item_count: 0, account_id: BSON::ObjectId('5bb52cc863623919f778f8aa')>, #<Folder _id: 5bb52d1b63623919f778f8b5, created_at: 2018-10-03 20:56:59 UTC, updated_at: 2018-10-03 20:56:59 UTC, name: "etc", path: "/accounts/5bb52cc863623919f778f8aa/sapiente/et", permissions: "ro", item_count: 0, account_id: BSON::ObjectId('5bb52cc863623919f778f8aa')>]

      So, searching in old issues I found a way to use 'elem_match' but when I tried It didnt work, as you can see below:

       

       

      2.5.1 :032 > folder_found = root.child_folders.elem_match(child_folders: { 'name' => 'dolorem'})
       => #<Mongoid::Criteria
        selector: {"child_folders"=>{"$elemMatch"=>{"name"=>"dolorem"}}}
        options:  {}
        class:    Folder
        embedded: true>
      
      2.5.1 :033 > folder_found.count
       => 0
      

      The thing is that this is a trivial query and I couldnt make it work, and I need to do this in a structure of n levels (basically look for a particular folder which could be inside of another folder) like this:

       

      /: (root_folder)
      --> 'xxxx' (child_folder)
      --> 'xxxx' (child_folder)
      --> 'xxxx' (child_folder)
        --> 'yyy' (child_folder)
          --> 'desire_folder' (child_folder)
      --> 'xxx' (child_folder)
      --> 'xxxx' (child_folder)

       

      I'm not sure if this is the correct way to query in recursive embeds many structure or if I need to design something by myself to resolve this.

      Hope to be clear enough and get any help that allows me to continue using the advantages of recursively_embeds_many.

       

       

            Assignee:
            oleg.pudeyev@mongodb.com Oleg Pudeyev (Inactive)
            Reporter:
            damianrobly damian
            Votes:
            1 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: