Extracting, modelling and changing data model, with mongoid/mongodb

心已入冬 提交于 2019-12-24 11:16:27

问题


I'm looking for a better way, if it's possible, for manipulating an existing mondodb datamodel using amazing mongoid ODM driver.

Suppose you have an embedded one to many data model like the following :

class User
  include Mongoid::Document

  field :nickname
  embeds_many :watchlists
end

class Watchlist
 include Mongoid::Document

 field :html_url
 field :description
 field :tags_array, type: Array
 embedded_in :user
end

Now, for all users you want to extract just a part of every watchlist, if and only if, it has

tags_array == ["ruby", "web", "framework"]

having back just few fields (not the entire watchlist doc):

  1. watchlist's html_url content
  2. watchlist's description content

and

  1. related parent nickname ( User.nickname )

I tried something like this :

1.9.2p290 :574 > Competitor = Struct.new(:html_url, :description, :user)
=> #<Class:0xed08de8>
1.9.2p290 :575 > competitors = []
=> []
1.9.2p290 :576 >  User.all.map do |user|
1.9.2p290 :577 >     user.watchlists.all.map do |wl|
1.9.2p290 :578 >           if wl.tags_array == ["ruby", "web", "framework"]
1.9.2p290 :579?>                 competitors << Competitor.new(wl.html_url, wl.description, wl.user.nickname)
1.9.2p290 :580?>           end
1.9.2p290 :581?>     end
1.9.2p290 :582?> end

here are some resoults :

1.9.2p290 :585 > competitors
=> [#<struct html_url="https://github.com/rails/rails", description="Ruby on Rails", user="lgs">, #<struct html_url="https://github.com/sinatra/sinatra", description="Classy web-development dressed in a DSL (official / canonical repo)", user="lgs">]
1.9.2p290 :586 > competitors.size
=> 2
1.9.2p290 :599 > competitors[0][:html_url]
=> "https://github.com/rails/rails"
1.9.2p290 :600 > competitors[1][:html_url]
=> "https://github.com/sinatra/sinatra"
1.9.2p290 :601 >

But I wonder, if there are better, smarter, faster, efficient, effective, aesthetic ( or just "different" ) way, of doing that ...


回答1:


You do two things:

  • Filter the users with db query instead of filtering in application
  • only fetch fields you need from db, rather than the whole user objects(assuming you have some other stuff in user, which you omitted here for brevity)

    Competitor = Struct.new(:html_url, :description, :user)
    competitors = []
    User.where('watchlists.tags_array' => %w[ruby web framework]).
        only(:nickname, :watchlists).each do |u|
      u.watchlists.where(:tags_array => %w[ruby web framework]).each do |wl|
        competitors << Competitor.new(wl.html_url, wl.description, u.nickname)
      end
    end
    

PS: Probably you do not want to use map on User.all, it will require a lot of memory if you have lots of heavy user documents. Also, you are not using the mapped users, but instead collecting results in the competitors array yourself, so each should work just fine.



来源:https://stackoverflow.com/questions/10121977/extracting-modelling-and-changing-data-model-with-mongoid-mongodb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!