我正在编写一个使用MongoDB和Mongoid的Rails应用程序的迁移.我的迁移目前使用我的模型使用Mongoid查询和更新记录,但性能低于标准.我本质上是更新大型集合中的所有记录并进行n + 20次查询.我花了一个小时在本地运行(并没有完成)后杀死了迁移.我希望能够毫不费力地将原始查询运行到mongo.我假设有一些方法可以从Mongoid访问mongo驱动程序,因为Mongoid已经加载了与数据库的连接.如何访问数据库以直接运行更新查询?
我想在Hash字段上查询Mongoid类.我不确定我怎么能用条件做到这一点?
这是一个例子:
class Person
include Mongoid::Document
field :things, :type => Hash
end
Run Code Online (Sandbox Code Playgroud)
那么,让我们说我有这个:
p = Person.new
p.things = {}
p.things[:tv] = "Samsung"
Run Code Online (Sandbox Code Playgroud)
我想查询第一个拥有三星电视的人......
People.first(:conditions => ?????
Run Code Online (Sandbox Code Playgroud)
提前致谢.
你如何检索一个IDsin 数组Mongoid?
arr=["id1","id2"]
User.where(:id=>arr)
Run Code Online (Sandbox Code Playgroud)
如果要检索其他属性,则可以轻松完成此操作
User.where(:nickname.in=>["kk","ll"])
Run Code Online (Sandbox Code Playgroud)
但我想知道如何在mongoid中做到这一点 - >这应该是一个非常简单和常见的操作
我无法超越这个.我知道我已经读过没有数组的页面方法,但我该怎么办?
如果我在控制台中运行Class.all,则返回#,但如果我运行Class.all.page(1),则会出现上述错误.
有任何想法吗?
有人能给我一个简短的介绍,使用Mongoid在Rails中进行数据库迁移吗?我对每个文档迁移的懒惰特别感兴趣.这意味着,无论何时从数据库中读取文档,都要将其迁移到最新版本并再次保存.
有没有人以前做过这种事情?我遇到过mongoid_rails_migrations,但它没有提供任何类型的文档,虽然它看起来像这样做,但我真的不确定如何使用它.
我应该指出,我只是在概念上熟悉ActiveRecord迁移.
使用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
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我正在尝试建模一个简单的游戏世界环境,其中地图嵌入位置,并且玩家将单个位置作为当前位置.
使用这种方法,当我尝试引用Player类的"location"属性时,我收到以下错误:
Mongoid::Errors::DocumentNotFound: Document not found for class Location with id(s) xxxxxxxxxxxxxxxxxxx.
Run Code Online (Sandbox Code Playgroud)
我的理解是,这是因为位置文档是嵌入式的,因此很难在其嵌入文档(Map)的范围之外引用.这是有道理的,但我如何建模对嵌入式文档的直接引用?
我正在使用rails 3,并使用ActiveRecord开始我的应用程序.现在,我有很多模型,关系开始变得复杂,有些可以用Document-Oriented结构更简单地表达,所以我想尝试迁移到MongoDB并使用Mongoid.
我一直听说你没有必要使用所有MongoDB或者没有任何东西,但你可以在迁移时并行使用这两个.我不知道如何从文档中解决这个问题.
例如,我有:
class User < ActiveRecord::Base
has_many :items
has_many :products, :through => :items
end
class Product < ActiveRecord::Base
has_many :items
end
class Item < ActiveRecord::Base
belongs_to :user
belongs_to :product
# alot of data that fits a hierarchical document-oriented structure
end
Run Code Online (Sandbox Code Playgroud)
我想最好用一个Mongoid文件替换我的项目ActiveRecord的模型开始,所以我的项目都存储在MongoDB中,我的Users和Products可以留在我的SQL数据库
事情是,我不知道该怎么做.我是以正确的方式来做这件事的吗?
也许另一种选择是保留基础AR项目
class Item < ActiveRecord::Base
has_one :mongodb_item ?? # I know this is wrong
end
class MongodbItem
include Mongoid::Document
belongs_to AR_Item ??? # I know this is also wrong
end
Run Code Online (Sandbox Code Playgroud)
谢谢!
inverse_of在mongoid协会中意味着什么?如果没有它,我可以通过使用它而不仅仅是关联来获得什么?
我有一个集合,集合中的每个文档都有一个名为foo包含一组嵌入文档的数组.MongoDB shell中目前有一种简单的方法来计算其中有多少个实例foo?就像是:
db.mycollection.foos.count()还是db.mycollection.foos.size()?
数组中的每个文档都需要具有唯一性foo_id,我想快速计算以确保在集合中的适当随机文档的数组中包含适量的元素.
mongoid ×10
mongodb ×6
ruby ×3
activerecord ×1
associations ×1
kaminari ×1
nosql ×1
relationship ×1