【问题标题】:Finding the size of a Mongo::Collection::View查找 Mongo::Collection::View 的大小
【发布时间】:2016-09-14 19:57:26
【问题描述】:

我正在尝试在 MongoDB 和 ruby​​ 中查找查询结果的大小:

size = collection.find({ foo: 'bar' }).size
if size > 0
  print "There are #{size} results!"
else
  print "There aren't any results."
end

但是,当我运行它时,我收到以下错误:

undefined method `size' for #<Mongo::Collection::View:0x00000004141d48>

如何正确确定此查询返回的项目数?我在文档中找不到任何信息。

【问题讨论】:

    标签: ruby mongodb mongodb-ruby


    【解决方案1】:
    collection.find({ foo: 'bar' }).count()
    

    应该可以解决您的问题。 mongo 中没有可用的 size 方法,但有 count。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多