如何使用 Mongoid 判断 MongoDB 中是否存在集合?

发布于 2024-09-25 01:06:19 字数 380 浏览 7 评论 0原文

由于即使集合不存在,Mongoid.master.collection()也会返回一个集合,因此我们可以用来

coll = Mongoid.master.collection('analyticsCachedResult')
if coll.count == 0
  # [...]
end

测试它是否是一个空集合。另一种方法是循环遍历

Mongoid.master.collections.each do |c|
  return c if c.name == 'analyticsCachedResult'
end
return nil

,但是有没有更简单的方法来检测是否存在呢?

Since Mongoid.master.collection() returns a collection even if the collection doesn't exist, we can use

coll = Mongoid.master.collection('analyticsCachedResult')
if coll.count == 0
  # [...]
end

to test if it is an empty collection. Another method is to loop through

Mongoid.master.collections.each do |c|
  return c if c.name == 'analyticsCachedResult'
end
return nil

but is there a simpler way to detect whether it exists?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

仅此而已 2024-10-02 01:06:19

不确定如何通过 Mongoid 执行此操作,但通常您可以在 system.namespaces 集合中查询 {name : "dbname.analyticsCachedResult"}

Not sure how to do it through Mongoid, but in general you can query the system.namespaces collection for {name : "dbname.analyticsCachedResult"}.

悲喜皆因你 2024-10-02 01:06:19

使用 mongo ruby​​ 驱动程序,我扩展了 DB 类:

class Mongo::DB
  def collection_exists?(collection_name)
    x = "#{self.name}.#{collection_name}" # e.g., "redbike_db.clients"
    nil != self['system.namespaces'].find_one({'name' => x})
  end
end

Using the mongo ruby driver, I extended the DB class:

class Mongo::DB
  def collection_exists?(collection_name)
    x = "#{self.name}.#{collection_name}" # e.g., "redbike_db.clients"
    nil != self['system.namespaces'].find_one({'name' => x})
  end
end
小糖芽 2024-10-02 01:06:19

对于 gem mongoid (8.0.2),我使用此代码

client = Mongoid.default_client
current_db = client.database

current_db.collection_names.include?("name of the collection you want to check")

For the gem mongoid (8.0.2), I use this code

client = Mongoid.default_client
current_db = client.database

current_db.collection_names.include?("name of the collection you want to check")
桃酥萝莉 2024-10-02 01:06:19

您可能正在寻找 Mongoid::Findable#exists ?

  • exists? 将评估 true 如果集合中至少有一个文档...

让我们假设这是您的模型:

class AnalyticsCachedResult
  include Mongoid::Document

  store_in collection: "analyticsCachedResult"

  class Foo
    include Mongoid::Document
    embedded_in :analytic_cached_result, inverse_of: :foos, class_name: "AnalyticsCachedResult"
  end

  embeds_many :foos, class_name: "AnalyticsCachedResult::Foo"
end
  • 其中 < code>Foo 嵌入分析文档中

虽然两个模型都可以引用集合,但这并不意味着它们将各自生成一个集合:

AnalyticsCachedResult.collection.name
# => "analyticsCachedResult"

AnalyticsCachedResult::Foo.collection.name
# => "analytics_cached_result_foos"

因此,当还没有文档时,存在吗?它将返回 false

AnalyticsCachedResult.exists?
# => false

AnalyticsCachedResult::Foo.exists?
# => false

现在让我们创建一个文档并保存它:

acr = AnalyticsCachedResult.new
acr.foos.build({})
acr.save!

您的顶级模型现在将评估 true

AnalyticsCachedResult.exists?
# => true

尽管有一个 AnalyticsCachedResult::Foo 文档,它嵌入 AnalyticsCachedResult 中存储,因此其集合将为空:

acr.fooes.first
# => #<AnalyticsCachedResult::Foo _id: 111122223333444455556666, >

AnalyticsCachedResult::Foo.exists?
# => false

You are probably looking for Mongoid::Findable#exists?

  • exists? will evaluate true if at least there is one document in the collection...

Let's assume this is your model:

class AnalyticsCachedResult
  include Mongoid::Document

  store_in collection: "analyticsCachedResult"

  class Foo
    include Mongoid::Document
    embedded_in :analytic_cached_result, inverse_of: :foos, class_name: "AnalyticsCachedResult"
  end

  embeds_many :foos, class_name: "AnalyticsCachedResult::Foo"
end
  • where Foo is embedded in the analytics document

While both models can refer to a collection, that does not mean they will generate a collection each:

AnalyticsCachedResult.collection.name
# => "analyticsCachedResult"

AnalyticsCachedResult::Foo.collection.name
# => "analytics_cached_result_foos"

So when there are no documents yet, exists? it will return false

AnalyticsCachedResult.exists?
# => false

AnalyticsCachedResult::Foo.exists?
# => false

Now let's create one document and persist it:

acr = AnalyticsCachedResult.new
acr.foos.build({})
acr.save!

Your top model will now evaluate true:

AnalyticsCachedResult.exists?
# => true

And although there is one AnalyticsCachedResult::Foo document, it is stored embedded in AnalyticsCachedResult, and therefore its collection will be empty:

acr.fooes.first
# => #<AnalyticsCachedResult::Foo _id: 111122223333444455556666, >

AnalyticsCachedResult::Foo.exists?
# => false
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文