Mongoid "find" returns nil,当"find_by" 检索记录时
Mongoid "find" returns nil, when "find_by" retrieves record
我正在使用 Mongoid 访问 MongoDB 数据库,但是我 运行 遇到了一个奇怪的问题。似乎我只能使用 find_by
查询记录,因为 find
将始终 return nil:
invoices = Invoice.find({})
p "invoices"
p invoices
puts ''
invoice = Invoice.find_by({ _id: <ObjectId> })
p "invoice"
p invoice
puts ''
使用 find_by
的第二个查询将 return 一条记录。根据 the documentation,find
应该 returning 满足查询的每条记录。
有人知道是什么原因造成的吗?
注意不要将 Moped 语法与 Mongoid 语法混淆。对于 Mongoid,文档描述了 find 方法:
Find a document or multiple documents by their ids. Will raise an error by default if any of the ids do not match
如果您真的想要每条记录,Invoice.all
可以做到。 (还要小心你的 find_by 方法。Mongoid 语法与 mongo 有一点不同,所以你不必在参数周围有花边。)
我正在使用 Mongoid 访问 MongoDB 数据库,但是我 运行 遇到了一个奇怪的问题。似乎我只能使用 find_by
查询记录,因为 find
将始终 return nil:
invoices = Invoice.find({})
p "invoices"
p invoices
puts ''
invoice = Invoice.find_by({ _id: <ObjectId> })
p "invoice"
p invoice
puts ''
使用 find_by
的第二个查询将 return 一条记录。根据 the documentation,find
应该 returning 满足查询的每条记录。
有人知道是什么原因造成的吗?
注意不要将 Moped 语法与 Mongoid 语法混淆。对于 Mongoid,文档描述了 find 方法:
Find a document or multiple documents by their ids. Will raise an error by default if any of the ids do not match
如果您真的想要每条记录,Invoice.all
可以做到。 (还要小心你的 find_by 方法。Mongoid 语法与 mongo 有一点不同,所以你不必在参数周围有花边。)