获取mongodb数据变更_使用Mongoid / mongodb提取,建模和更改数据模型

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):

watchlist's html_url content

watchlist's description content

and

related parent nickname ( User.nickname )

I tried something like this :

1.9.2p290 :574 > Competitor = Struct.new(:html_url, :description, :user)

=> #<0xed08de8>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

=> [#, #]

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 ...

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值