Tag: mongoid

如何在Mongoid中引用嵌入式文档?

使用Mongoid,假设我有以下类: class Map include Mongoid::Document embeds_many :locations end class Location include Mongoid::Document field :x_coord, :type => Integer field :y_coord, :type => Integer embedded_in :map, :inverse_of => :locations end class Player include Mongoid::Document references_one :location end 正如您所看到的,我正在尝试建模一个简单的游戏世界环境,其中地图嵌入位置,并且玩家将单个位置作为当前位置。 使用这种方法,当我尝试引用Player类的“location”属性时,我收到以下错误: Mongoid::Errors::DocumentNotFound: Document not found for class Location with id(s) xxxxxxxxxxxxxxxxxxx. 我的理解是,这是因为位置文档是嵌入式的,因此很难在其嵌入文档(Map)的范围之外引用。 这是有道理的,但我如何建模对嵌入式文档的直接引用?

如何使用Ruby和MongoId正确保存时区?

如果这是一个noob问题,请原谅我: 我有一个应用程序,用户可以在他们的个人资料中设置自己的时区。 当有人添加阵容(特定于应用程序的术语)时,我会执行以下操作: time = ActiveSupport::TimeZone.new(user.timezone).parse( “Wednesday, 26 October, 2011 13:30:00” ) # This outputs: 2011-10-26 13:30:00 +0200 – valid according to the user selected TZ 然后我保存阵容: Lineup.create({ :date => time.gmtime, :uid => user._id, :pid => product._id }) 这应该(理论上)将日期保存为gmtime,但在查看记录时我会得到以下信息: { “_id”: ObjectId(“4e9c6613e673454f93000002”), “date”: “Wed, 26 Oct 2011 13: 30: 00 +0200”, “uid”: “4e9b81f6e673454c8a000001”, “pid”: “4e9c6613e673454f93000001”, […]

MongoDB / Mongoid和Rails 3的日期时间没有填充

这是我的模型中的代码 include Mongoid::Document include Mongoid::Timestamps field :message, :type => String field :send_at, :type => DateTime 这是我的表格部分的代码 但是从不填充日期和时间。 我确保Mongo和Mongoid也是最新的。 不确定是否有我遗漏的东西。 [更新日志条目] Started POST “/notifis” for 127.0.0.1 at Mon Oct 18 05:48:05 -0400 2010 Processing by NotifisController#create as HTML Parameters: {“commit”=>”Create Notifi”, “authenticity_token”=>”/hrlnvA2Xn5NqGgCkPFAQV254IHPJEvZoLxOYNNUwhc=”, “_snowman”=>”☃”, “notifi”=>{“send_at(2i)”=>”10”, “is_sent”=>”0”, “send_at(3i)”=>”18”, “send_at(4i)”=>”09”, “message”=>”erwer”, “send_at(5i)”=>”48”, “send_at(1i)”=>”2010”}} MONGODB noti_development[‘notifis’].insert([{“send_at(2i)”=>”10”, “created_at”=>Mon Oct 18 09:48:05 […]

在Rails中使用ActiveModel :: Serializer – JSON数据在json和index响应之间有所不同

我正在使用active_model_serializers gem来控制序列化数据,并看到一些奇怪的行为。 我的代码看起来像这样: 模型和序列化器 class User include Mongoid::Document field :first_name, :type => String field :last_name, :type => String def full_name first_name + ” ” + last_name end end class UserSerializer < ActiveModel::Serializer attributes :id, :first_name, :last_name, :full_name end 调节器 class UsersController < ApplicationController respond_to :json, :html def index @users = User.all respond_with @users end end […]

如何使用MongoDB Ruby Driver进行“分组”(分组)?

使用Ruby驱动程序与MongoDB Group相关 如果我想在SQL中执行以下操作: select page_id, count(page_id) from a_table group by page_id 我认为MongoDB的文档说 http://api.mongodb.org/ruby/current/Mongo/Collection.html#group-instance_method group(key, condition, initial, reduce, finalize = nil) # returns an array 所以从另一篇文章中,我正在使用: Analytic.collection.group( “fucntion (x) return {page_id : x.page_id}”, nil, {:count => 0}, “function(x, y) { y.count++ }” ) 但它实际上会回来 [{“count”=>47.0}] 这是集合中记录(文档)的总数。 以上是不正确的? 我以为密钥可能是一个静态字符串 http://kylebanker.com/blog/2009/11/mongodb-count-group/ db.pageviews.group( { key: {‘user.agent’: true}, initial: {sum: […]

检索Mongoid中的id数组

你如何在Mongoid检索一个IDs数组? arr=[“id1″,”id2”] User.where(:id=>arr) 如果要检索其他属性,则可以轻松完成此操作 User.where(:nickname.in=>[“kk”,”ll”]) 但我想知道如何在mongoid中做到这一点 – >这应该是一个非常简单和常见的操作

Mongoid:通过数组ID查找

我通过MapReduce获取了一些ID。 我按照一些标准对这些ID进行了排序,现在我需要按照这个特定的顺序获取这些对象: MyModel.find(ids) 对? 但它返回的对象不是存储的id。 看起来像是一样的 MyModel.where(:_id.in => ids) 它不会以与存储的id相同的顺序返回获取的对象。 现在我可以做到这一点 ids.map{|id| MyModel.find(id)} 这将完成这项工作,但它将多次敲击数据库。

如何validation数组字段的成员?

我有这个型号: class Campaign include Mongoid::Document include Mongoid::Timestamps field :name, :type => String field :subdomain, :type => String field :intro, :type => String field :body, :type => String field :emails, :type => Array end 现在我想validationemailsarrays中的每封emails是否格式正确。 我读了Mongoid和ActiveModel :: Validations文档,但我没有找到如何做到这一点。 你能告诉我指针吗?

Ruby模型输出id作为对象oid

我的ruby模型,如下: class User include Mongoid::Document field :first_name, type: String field :birthdate, type: Date validates :first_name, :birthdate, :presence => true end 输出一个像这样的对象: { _id: { $oid: “522884c6c4b4ae5c76000001” }, birthdate: null, first_name: null, } 我的骨干项目不知道如何处理_id。$ oid。 我发现这篇文章和代码: https://github.com/rails-api/active_model_serializers/pull/355/files module Moped module BSON class ObjectId alias :to_json :to_s end end end 我不知道在哪里放这个,以及如何在模型输出上调用它,所以我尝试了内部: /config/initializers/secret_token.rb 我是Ruby和Rails的新手,不知道如何继续,所以非常感谢任何帮助

mongoid查询缓存

Rails的ActiveRecord有一个名为Query Caching(ActiveRecord :: QueryCache)的function,可以保存请求生命周期的SQL查询结果。 虽然我不太熟悉实现的内部,但我认为它将查询结果保存在Rack env中,在请求结束时将其丢弃。 遗憾的是,Mongoid目前还没有提供这样的function,而且有些查询是隐式发生的(引用),这更加恶化了。 我正在考虑实现这个function,我很好奇,Mongoid(或者,也许是mongo驱动程序?)的位置和方式应该被挂钩以实现它。