Mongoid在最后一次调用时返回第一个文档的错误? [英] The bug of mongoid returning first document when invoking last?

查看:65
本文介绍了Mongoid在最后一次调用时返回第一个文档的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了mongoid错误,当我调用方法last

I encountered a bug of mongoid, which return first document when I invoking the method last

class Post
  include Mongoid::Document
end

Post.create
Post.create
Post.first == Post.last #=> true

版本信息:

  • 蒙古语:"5.0.2"
  • mongodb:v3.2.1
  • 导轨4.2.5

推荐答案

这不是Mongoid中的错误,而是您对firstlast方法的期望中的错误.从精细版本5手册:

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在最后一次调用时返回第一个文档的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆