MongoDB中的常见做法是使用短密钥名称来节省空间.例如,有人可能想使用"fn"而不是"first_name"
但是在你的应用程序中,如果你在整个地方使用"fn",你就会被搞砸了.太难看了.特别是对于Rails,在Mongoid中声明字段时是否有一种简单的方法来指定别名?
另外,有没有人知道任何使用Mongoid的开源示例项目?
谢谢!
我有点卡在这里(mongoid docs似乎没有给出答案)
Question.where(:text.contains=>"perfect")
Run Code Online (Sandbox Code Playgroud)
我想找到哪些文本字段包含给定单词的问题,在这种情况下,完美.
这里的查询是什么,以及如何提高此类查询的性能?
我想按月对帖子进行分页,所以我在Post模型中添加了以下范围
class Post
include Mongoid::Document
include Mongoid::Timestamps
scope :by_month, lambda {|end_date| Post.order_by(:created_at => :asc).where(:created_at.gte => (end_date.to_date.beginning_of_month), :created_at.lte => (end_date.to_date))}
end
Run Code Online (Sandbox Code Playgroud)
在我的控制器中我放了
def show
@posts = Post.by_month(Time.now).page(params[:page]).per(20)
end
Run Code Online (Sandbox Code Playgroud)
在视野中
<%= paginate @posts, :theme => 'month_theme' %>
<%= render @posts %>
Run Code Online (Sandbox Code Playgroud)
问题:
使用Rails 3.1.1让我的资产管道设置与Heroku/Cedar一起使用时遇到了一些麻烦
我已推送我的应用程序并成功启动,但没有消息说"准备资产管道"并且没有提供静态资产.由于没有slug编译时间或运行时资产编译,因此无法找到JS,CSS或图像.
任何帮助将不胜感激.
他们描述了在此链接部署期间应该发生的事情,我将在下面总结:
使用Heroku Cedar上的Rails 3.1应用程序,当你git push heroku时,你的资产将通过使用rake任务包exec rake assets:precompile预先编译为部署过程的一部分.
我正在试图找出管理特定用例的控制器和模型的最佳方法.
我正在建立一个评论系统,用户可以使用Polymorphic Reviewable建立对几种不同类型的评论.
Country (has_many reviews & cities)
Subdivision/State (optional, sometimes it doesnt exist, also reviewable, has_many cities)
City (has places & review)
Burrow (optional, also reviewable ex: Brooklyn)
Neighborhood (optional & reviewable, ex: williamsburg)
Place (belongs to city)
Run Code Online (Sandbox Code Playgroud)
我也想知道增加更多的复杂性.我还想偶尔包括细分...即对于美国,我可能会添加德克萨斯州或德国,Baveria并且也可以审查它,但并非每个国家都有地区,甚至那些可能永远不会被审查的地区.所以它一点也不严格.我希望它尽可能简单灵活.
如果用户可以在一个表单上登陆并选择城市或国家,然后使用Foursquare中的数据进行深入查找以查找城市中的特定位置并进行审核,那就太好了.
我真的不确定我应该走哪条路?例如,如果我有一个国家和一个城市会发生什么......然后我决定添加一个陋居?
我可以将地点标签(即威廉斯堡,布鲁克林)归属于纽约市并且标签属于纽约吗?
标签更灵活,可选择解释它们可能属于哪些区域,标签属于某个城市,但也有地点和可审核?
所以我正在为那些做过相关事情的人寻找建议.
使用Rails 3.2和mongoid.
当我尝试查询Mongoid标准的结果并仅保留字段不同的文档时,我感到非常沮丧.这样做:
Books.all.distinct(:name)
Run Code Online (Sandbox Code Playgroud)
..only返回名称字段,而不是文档.
另外使用uniq另一个问题中所述的循环对我来说不起作用.
Books.all.uniq{|x| x.name} # Returns non-unique results
Run Code Online (Sandbox Code Playgroud)
我在这里错过了什么?
我有两个型号Content和ContentType.在内容模型中,我可以这样做:
def get_all_content_except_poking_message
Content.all.where(:name.ne => "no forking, just poking")
end
Run Code Online (Sandbox Code Playgroud)
现在,我正在尝试在ContentType上应用范围.在内容模型中再次:
# Associations
belongs_to :content_type
def get_all_content_except_certain_content_type(content_type)
Content.all.where(:content_type.name.ne => content_type)
end
Run Code Online (Sandbox Code Playgroud)
但错误表明它在关联字段上应用范围的语法错误.
在模型中的关联字段上应用范围的正确方法是什么?
我也在使用has_scope gem.我也可以在控制器中应用相同的过滤器吗?就像是:
@contents = apply_scopes(
if params[:type]
@content_type = ContentType.find_by_slug(params[:type])
@content_type.contents.all
else
Content.all.where (:content_type.name.ne => "blogs")
end
)
Run Code Online (Sandbox Code Playgroud)
为了澄清,这里是irb输出:
irb(main):020:0> ContentType.all(:name=>"blogs").count
=> 1
irb(main):023:0> Content.last.content_type.name
=> "blogs"
irb(main):024:0> Content.all.where(:content_type => {:name => {'$ne' => "blogs"}}).count
=> 0
irb(main):026:0> Content.all.count
=> 4
Run Code Online (Sandbox Code Playgroud) model-view-controller ruby-on-rails mongodb mongoid has-scope
我是MongoDB和Mongoid的新手,我正在使用Debian测试(jessie/sid).
当我打开时/etc/mongodb.conf,没有关于Mongoid存储数据库和日志的信息.
它只是提到 logpath=/var/log/mongodb/mongodb.log
另外config/mongoid.yml没有有用的信息:
development:
sessions:
default:
hosts:
- localhost:27017
database: project_development
Run Code Online (Sandbox Code Playgroud)
如何project_development在磁盘上找到db并为该数据库记录日志?那个地方有设置吗?
我正在写一些测试,但我遇到了一些我想要了解的东西.
调用时有什么区别:
.update_attributes(:group_ids, [group1.id, group2.id])
Run Code Online (Sandbox Code Playgroud)
VS
.update_attributes(:groups, [group1, group2])
Run Code Online (Sandbox Code Playgroud)
这两个模型有问题:
group.rb
class Group
include Mongoid::Document
has_and_belongs_to_many :users, class_name: "Users", inverse_of: :groups
end
Run Code Online (Sandbox Code Playgroud)
user.rb
class User
include Mongoid::Document
has_and_belongs_to_many :groups, class_name: "Group", inverse_of: :users
end
Run Code Online (Sandbox Code Playgroud)
有问题的测试代码:
g1 = create(:group)
u1 = create(:user, groups: [g1])
g1.update_attribute(:users, [u1])
# at this point all the associations look good
u1.update_attribute(:group_ids, [g1.id])
# associations looks good on both sides when i do u1.reload and g1.reload
u1.update_attribute(:groups, [g1])
# g1.reload, this is when g1.users is empty and …Run Code Online (Sandbox Code Playgroud) 我有一个执行这样的事情的查询,
last_shipment_id = OrderDelivery.where(platform: 'business').desc(:shipment_id).limit(1).pluck(:shipment_id)[0]
Run Code Online (Sandbox Code Playgroud)
当我正确索引{platform:1,shipment_id:-1}而没有在分段机器中分割环境时,它只有1~5ms的效果很好
但是,我们的生产设置了4个分片mongo db,结果最终在1000~3000ms.
有谁知道这可能会如何发生或如何解决这种情况?
我已经阅读过这张幻灯片https://www.slideshare.net/mongodb/how-queries-work-with-sharding
好吧,它在幻灯片13中说,但仍然不确定它是否已经提到如何解决这个案例.