Tag: mongodb

Mongo :: Error :: OperationFailure:E11000重复键错误集合:metalab_development.demands index:sn_1 dup key:{:null}(11000)

为什么我遇到错误’重复键’? 实际上,我在代码中添加了以下行: index({ sn: 1 }, { unique: true, sparse: true}) 然后我输入命令: rake db:mongoid:create_indexes 出现了错误,让我感到困惑。

mongoid – mongodb,mapreduce获取范围可变或将其传递给输出

在mapreduce中,我有一个范围total_count,用于在分组内容后计算一些东西。 如何在输出中恢复该范围可变? 编辑: map = %Q{ function() { ttotal_commission += this.commission; tuniq_commission += this.commission; // add first if it not goto reduce emit({ip: this.ip, campaign_id: this.campaign_id}, {commission: this.commission, abc: this.commission}); } } reduce = %Q{ function(key, values) { tuniq_commission += values[0].commission; // add 1 and substract laster for uniq var total_commission = 0; values.forEach(function(value) { […]

Mongoid Association创建(不需要的)记录

我不知道Mongoid为何在协会创造新纪录。 我正在密切关注代码,但我从来没有见过这样的东西。 我做了一个测试并减少了代码。 我离开录像机以防它可能是相关的。 它“不应该为MT创建重复的条目”do state = PolcoGroup.create(type :: state,name:’MT’,active:true)s = state.get_senators state.junior_senator = s [:state_junior_senator]#!! ! 这会创建一个新记录state.senior_senator = s [:state_senior_senator]#!!!!! 所以这行预期(Legislator.all.size).to eql(2)#实际上等于4 – 每个关联创建一个新的记录最终结果是:Legislator.all.map(&:sortname)=> [“Tester,乔恩(参议员)[D-MT]“,”沃尔什,约翰(参议员)[D-MT]“,”沃尔什,约翰(参议员)[D-MT]“,”测试员,乔恩(参议员) [D-MT]“] ##模型类PolcoGroup包括Mongoid :: Document包括Mongoid :: Timestamps包括VotingMethods包括DistrictMethods扩展DistrictClassMethods包括StateMethods字段:name,:type => String … #STATE RELATIONSHIPS —– ———————— has_one:junior_senator,class_name:“Legislator”,inverse_of :: jr_legislator_state has_one:senior_senator,class_name:“Legislator”,inverse_of: :sr_legislator_state … end class立法者包括Mongoid :: Document包括Mongoid :: Timestamps#以下字段直接来自govtrack字段:govtrack_id,类型:整数字段:bioguideid,类型:字符串… belongs_to:jr_legislator_state,class_name:“ PolcoGroup“,inverse_of :: junior_senator […]

Rails,MongoMapper在HAML中嵌套数组形成麻烦

我越来越 用于#的未定义方法`胡萝卜’ (引用ln#18) 尝试使用以下表单进行编辑时: = form_for @harvest do |f| – if @harvest.errors.any? #error_explanation %h2= “#{pluralize(@harvest.errors.count, “error”)} prohibited this harvest from being saved:” %ul – @harvest.errors.full_messages.each do |msg| %li= msg .field = f.label :created_at = f.text_field :created_at, :disabled => true %br = f.label :photo = f.text_field :photo %h2 Crops – @harvest.harvested_crops.each do |harvested_crop| = f.label :harvested_crop[‘crop’] […]

基于Mongoid计算字段的查询

我想在模型Invoice实施一个overdue范围,以返回超过日期的所有发票,直到他们必须付款为止。 我有字段invoice_date, :type => Date和days_for_payment, :type => Integer 。 在我之前的版本(基于ActiveRecord构建)中,我可以使用该查询 Invoice.where(“invoice_date + days_for_payment < ?", Date.today) 此查询在DB端进行了计算。 有没有办法让Mongoid完成同样的事情? 或者有没有人知道一个好的解决方法(proc,lambda等)? 我使用mongoid’2.4.12′

有时候在monogid中删除文档

我正在使用mongoid的rails app。 这是模型(集合)之一: class Document include Mongoid::Document include Mongoid::Timestamps include Mongoid::Paperclip field :time, type: Time field :radius, type: Float attr_accessible :time,:radius end 时间字段包含应删除模型的实际时间。 我怎么能这样做,我有一个想法,我必须写一个脚本把它放在cron工作。 但我不想创建一个cron工作。 有没有其他方法可以自动化这个或我在轨道模型中的任何方法我可以定义或仅内置到rails中的东西。 我肯定错过了一些东西。

使用mongodb作为数据库的rails中的关联

我正在使用设计。 它将当前用户标识赋予 current_user.id 有很多用户。 控制器名称为empsals_controller.rb class EmpsalsController < ApplicationController def index @empsals = Empsal.all end def show @empsal = Empsal.find(params[:id]) end def new @empsal = Empsal.new end def edit @empsal = Empsal.find(params[:id]) end def create @empsal = Empsal.new(params[:empsal]) respond_to do |format| if @empsal.save format.html { redirect_to @empsal, notice: 'Empsal was successfully created.' } format.json { […]

mongo + passenger:从rails中重新连接到数据库的正确方法? 如何确保最佳的mongo性能?

我们在轨道3.2.12,乘客+ nginx(1.0.10),mongo 2.2和mongomapper 0.12。 我们被告知乘客和mongo不能很好地协同工作,除非有一个配置集来确保快速的数据库连接(或重新连接)。 但是,有人建议我们在这里使用代码: http : //pastie.org/572282 。 然后我们在stackoverflow( mongoDB,乘客和性能问题与phusion乘客 )上找到了这篇文章,这是不同的。 这是确保乘客(和nginx)达到最佳mongo性能的正确方法吗?

Rails g为mongoid命令失败

我试图使用ruby-1.9.2使用以下命令从rails 3.2应用程序初始化mongoid: rails g mongoid:config 当我发出命令时,我收到以下错误: /Users/paulcowan/.rvm/gems/ruby-1.9.2-p290/gems/bundler-1.0.18/lib/bundler/rubygems_integration.rb:143:in block in replace_gem’: railties is not part of the bundle. Add it to Gemfile. (Gem::LoadError) from /Users/paulcowan/.rvm/gems/ruby-1.9.2-p290/bin/rails:18:in block in replace_gem’: railties is not part of the bundle. Add it to Gemfile. (Gem::LoadError) from /Users/paulcowan/.rvm/gems/ruby-1.9.2-p290/bin/rails:18:in block in replace_gem’: railties is not part of the bundle. Add it to Gemfile. […]

ActionView :: Template :: Error(在heroku上没有将Fixnum隐式转换为String

我正在使用Mongodb.App正在使用ROR应用程序在localhost上正常工作,但是当我在heroku中部署它时,我收到错误。 我的一个视图文件命令是这样的: @h=Portfolio.last.folders Portfolio是一个将字段文件夹作为数组的集合。 生产中的Mongodb包含这个。 即使我尝试在heroku rails控制台中做同样的事情我也得到了同样的错误: TypeError: no implicit conversion of Fixnum into String from /app/vendor/bundle/ruby/2.0.0/gems/moped-1.4.3/lib/moped/protocol/commands/authenticate.rb:35:in `+’ from /app/vendor/bundle/ruby/2.0.0/gems/moped-1.4.3/lib/moped/protocol/commands/authenticate.rb:35:in `digest’ from /app/vendor/bundle/ruby/2.0.0/gems/moped-1.4.3/lib/moped/protocol/commands/authenticate.rb:47:in `build_auth_command’ from /app/vendor/bundle/ruby/2.0.0/gems/moped-1.4.3/lib/moped/protocol/commands/authenticate.rb:25:in `initialize’ from /app/vendor/bundle/ruby/2.0.0/gems/moped-1.4.3/lib/moped/node.rb:488:in `new’ from /app/vendor/bundle/ruby/2.0.0/gems/moped-1.4.3/lib/moped/node.rb:488:in `login’ from /app/vendor/bundle/ruby/2.0.0/gems/moped-1.4.3/lib/moped/node.rb:48:in `block in apply_auth’ from /app/vendor/bundle/ruby/2.0.0/gems/moped-1.4.3/lib/moped/node.rb:47:in `each’ from /app/vendor/bundle/ruby/2.0.0/gems/moped-1.4.3/lib/moped/node.rb:47:in `apply_auth’ from /app/vendor/bundle/ruby/2.0.0/gems/moped-1.4.3/lib/moped/cluster.rb:250:in `with_secondary’ from /app/vendor/bundle/ruby/2.0.0/gems/moped-1.4.3/lib/moped/session/context.rb:104:in `with_node’ from /app/vendor/bundle/ruby/2.0.0/gems/moped-1.4.3/lib/moped/session/context.rb:43:in `query’ from /app/vendor/bundle/ruby/2.0.0/gems/moped-1.4.3/lib/moped/query.rb:115:in […]