Ruby 如何在Mongoid中快速查询多个集合?
Foo、Bar和Baz都是Mongoid系列车型。数据库中只有集合 有没有更有效的方法来获取数据库中的所有数据Ruby 如何在Mongoid中快速查询多个集合?,ruby,mongoid,Ruby,Mongoid,Foo、Bar和Baz都是Mongoid系列车型。数据库中只有集合 有没有更有效的方法来获取数据库中的所有数据 Foo.all + Bar.all + Baz.all 我不知道一种同时查询几个集合的方法,我相信如果有人尝试这样做,就会破坏使用像MongoDB这样的非SQL数据库的目的 但是,在MongoDB中有一种方法可以归档这种行为。使用嵌入式文档。例如: class Everything include Mongoid::Document include Mongoid::
Foo.all + Bar.all + Baz.all
我不知道一种同时查询几个集合的方法,我相信如果有人尝试这样做,就会破坏使用像MongoDB这样的非SQL数据库的目的 但是,在MongoDB中有一种方法可以归档这种行为。使用嵌入式文档。例如:
class Everything
include Mongoid::Document
include Mongoid::Timestamps
embeds_many :foos, class_name: 'Foo', inverse_of: :everything
embeds_many :bars, class_name: 'Bar', inverse_of: :everything
embeds_many :bazs, class_name: 'Bazs', inverse_of: :everything
end
class Foo
include Mongoid::Document
field :foo, type: String
embedded_in :everything, class_name: 'Everything', inverse_of: :foos
end
class Bar
include Mongoid::Document
field :bar, type: String
embedded_in :everything, class_name: 'Everything', inverse_of: :bars
end
class Bazs
include Mongoid::Document
field :baz, type: String
embedded_in :everything, class_name: 'Everything', inverse_of: :bazs
end
执行
所有操作。all
将在一次调用中检索所有操作中的所有文档以及所有嵌入的文档。据我所知,MongoDB中没有类似于表的东西。然而,collections
可能被视为RDBMS中表的倒数。是的,我指的是collections。