ruby on rails - The bug of mongoid returning first document when invoking last? -
i encountered bug of mongoid, return first document when invoking method last
class post   include mongoid::document end  post.create post.create post.first == post.last #=> true the versions info:
- mongoid: "5.0.2"
- mongodb: v3.2.1
- rails 4.2.5
that's not bug in mongoid, that's bug in expectations of first , last methods. fine version 5 manual:
#first ⇒ document
note: mongoid added
_idsort when no sort parameters provided explicitly user. caused bad performance issues , not expected,#first/#lastno longer guarantee order if no sorting parameters provided. order guarantees - sort must explicitly provided.get first document in database criteria's selector.
so first (as of mongoid5) gives first document in query with respect current order no longer supplies default order. you'll need supply own sort order in query if want first behave used to.
similarly last.
Comments
Post a Comment