That's not a bug in Mongoid, that's a bug in your expectations of the first and last methods. From the fine version 5 manual: #first⇒文档 注意:Mongoid以前在用户未明确提供排序参数的情况下添加了_id排序.这会导致性能问题,这是无法预料的,因此,如果未提供排序参数,则#first/#last将不再保证顺序.对于订单保证-必须明确提供一种排序. Note: Mongoid previously added an _id sort when no sort parameters were provided explicitly by the user. This caused bad performance issues and was not expected, so #first/#last will no longer guarantee order if no sorting parameters are provided. For order guarantees - a sort must be explicitly provided.为条件的选择器获取数据库中的第一个文档.Get the first document in the database for the criteria's selector.因此,first(从Mongoid5开始)相对于当前订单为您提供了查询中的第一个文档,但不再提供默认订单.您需要提供自己的 sort first像以前一样运行,请在查询中使用a>顺序.So first (as of Mongoid5) gives you the first document in the query with respect to the current order but it no longer supplies a default order. You'll need to supply your own sort order in the query if you want first to behave like it used to.类似于 last . 这篇关于Mongoid在最后一次调用时返回第一个文档的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-31 21:46