0

我正在尝试在 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>

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

4

1 回答 1

0
collection.find({ foo: 'bar' }).count()

应该可以解决您的问题。mongo 中没有可用的大小方法,但有计数。

于 2016-09-14T20:22:51.823 回答