草庐IT

mongoid3

全部标签

ruby-on-rails - mongoid : deal with concurrent find_or_create_by

在mongoid中使用find_or_create_by时有没有办法处理并发?我正在使用Tag.find_or_create_by(name:"foo")来标记我的应用程序中的一些项目。在Tag模型中,我还使用了唯一性验证:validates_uniqueness_of:name但是,当用户在短时间内发送多个帖子请求时,我的数据库中就会出现重复的标签。我想我明白为什么会发生这种行为,但我不知道如何让它按预期工作。任何想法?谢谢! 最佳答案 我会将其翻转为两个操作以使其成为原子操作:1)查找2)如果未找到则UPSERT编辑:或者,使用

ruby-on-rails - 如何使用 mongoid/mongodb 批量更新/更新插入?

我有一个包含数百万个Order文档的数据库。我使用以下方法批量插入它们:Order.collection.insert([{:_id=>BSON::ObjectId('5471944843687229cdfb0000'),:status=>"open",:name=>"Benny"},{:_id=>BSON::ObjectId('5471944843687229cdfc0000'),:status=>"open",:name=>"Allan"}])我经常需要更新订单的status属性。使用update_attribute方法单独更新它们是一种低效的方法。如何批量更新多个MongoDB文

ruby-on-rails - 如何检查 Mongoid 数组字段是否包含另一个数组中的一个或多个项目?

如果该字段是一个整数,则以下工作:User.where(id:[1,3,5])但是如果字段是数组呢?例如,用户有一个字段是favorite_numbers的数组:如何找到favorite_number为1、3或5的用户?rails4.1.7Mongoid5.0.0编辑:添加了Mongoid。 最佳答案 我不确定Mongoid5,但$in运算符应该负责展开右侧:User.where(:favorite_numbers.in=>[1,3,5])#orUser.where(favorite_numbers:{:$in=>[1,3,5]})

ruby-on-rails - Mongoid 5 : find_one_and_update with returnNewDocument

Mongoidv5.1.2是否有可能在与find_one_and_update一起使用时忽略returnNewDocument选项?考虑以下代码:next_number=TrackingId.where(id:id).find_one_and_update({:$inc=>{auto_increment_counter:1}},upsert:true,returnNewDocument:true).auto_increment_counter其中auto_increment_counter是Integerfield:auto_increment_counter,type:Integer

索引属性上的 Mongodb/Mongoid 查询缓慢

我有一个像这样的mongoid模型:classLinkincludeMongoid::DocumentincludeMongoid::Timestampsfield:url,type:Stringindex:url,background:trueend现在我有2个打开分析的查询:Link.where(url:"http://stackoverflow.com/questions/ask").first#=>#执行,没有慢的记录Link.where(url:"nourl").first#=>#nil执行=35毫秒*PROFILER::*10月9日星期日23:36:20[conn20]查询

ruby-on-rails - Mongoid:对 MongoDB 的评论和 MySQL 上的用户?如何让它发挥作用

我想创建一个同时使用MongoDB和MySQL的应用程序。具体来说,我希望mongodb存储所有用户的评论,而MySQL将存储用户模型。classUser好吧,一切看起来都不错,除非我转到Rails控制台并运行它。k=Comment.newk.user=User.first我得到了NoMethodError:UserLoad(0.3ms)SELECTusers.*FROMusersWHEREusers._id=1Mysql2::Error:Unknowncolumn'users._id'in'whereclause':SELECTusers.*FROMusersWHEREusers._

ruby-on-rails - 如何索引 MongoDB 或 Mongoid 中的哈希字段?

我有以下mongo文档:{_id:'someid',name:'JohnDoe',address:{city:'Osaka',country:'Japan'}}如何按城市和国家编制索引? 最佳答案 来自MongoDBdocumentation:IndexingonEmbeddedFieldsYoucancreateindexesonfieldsembeddedinsub-documents,justasyoucanindextop-levelfieldsindocuments.[...]Instead,indexesonembedd

ruby-on-rails - 如何处理 Rspec 中的 Mongoid::Errors::DocumentNotFound?

我有一个带有以下代码的ArticleController:defedit@article=current_user.articles.find(params[:id])end以及以下测试:describe"GET'edit'"doit"shouldfailwhensignedinandeditinganotheruserarticle"dosign_in@userget:edit,:id=>@another_user_article.idresponse.should_notbe_successendend但是,当我开始测试时,出现以下错误(这是正常的),但我想知道如何处理才能让我的测

ruby-on-rails - Mongoid 和嵌入标签

我有几个模型,每个模型都有标签。例如,一个User和一个Post。我认为两者都应该有嵌入式标签。用户的嵌入式标签是他们的“收藏夹”,帖子的标签是与帖子相关的标签。我正在使用Mongoid,但我不知道如何拥有一个可以找到所有标签的Tag模型(这些标签没有嵌入,我想要一个单独的集合所有可用的标签)。当用户尝试添加标签时,系统会根据Tag.all检查它以确保它存在。如果是,则应将其嵌入到User模型中。我不知道如何将Tag模型嵌入多个模型(User和Post)或如何嵌入Tag模型以及检索它们正常(我收到错误:不允许访问Tag的集合,因为它是嵌入文档,请从根文档访问集合。)。想法?我设计这个完

ruby-on-rails - 如何为mongoid设置时区

我正在为我的Rails应用程序使用mongoid。我想为mongoid设置时区。我在application.rb中尝试过这个config.mongoid.time_zone='Kolkata'但运气不好。我也试过这个use_utc:falseuse_activesupport_time_zone:true它仍然使用默认时区UTC。rails3.0.10mongoid2.2.4请帮忙。提前致谢。 最佳答案 像这样在application.rb中config.time_zone='Kolkata'在mongoid.yml中use_act