이것은 mongoid (3.0.0)를 사용하여 발견 한 재미있는 패턴으로, 버그입니다.Mongoid :: Criteria # last가 기준을 수정하고 잘못된 결과를 나타냅니다.
1.9.3p194 :007 > products = Product.order_by([:_id, :asc ]).limit(5)
=> #<Mongoid::Criteria
selector: {},
options: {:sort=>{"_id"=>1}, :limit=>5},
class: Product,
embedded: false>
1.9.3p194 :008 > products.map(&:_id)
=> ["500fa5614f6d3a23d0000002", "500fa5614f6d3a23d0000003", "500fa5614f6d3a23d0000004", "500fa5614f6d3a23d0000005", "500fa5614f6d3a23d0000006"]
지금까지 너무 좋아! 그러나, 내가 다음을 발행하면 - 나는 이상한 결과를 얻는다.
1.9.3p194 :012 > products.count
=> 3654017
이 제품이 대신 5의 개수 나에게 모든을 보여줍니다 (내가 갖고 있기 때문에 : 제한 => 5) 이제
1.9.3p194 :012 > Product.count
=> 3654017
짝수 더 이상한 부분 :
1.9.3p194 :010 > products.last
=> #<Product _id: 504952620a5e2323460000aa, _type: nil, ... >
을 이 번호는 _id : 500fa5614f6d3a23d0000006이어야합니다. 이제 ID를 다시 매핑하려고하면 다음과 같이 나타납니다.
1.9.3p194 :019 > products.map(&:id)
=> ["504952620a5e2323460000aa", "504952620a5e2323460000a9", "504952620a5e2323460000a8", "5049524f0a5e2323460000a7", "504950ab0a5e2323460000a6"]
이것은 완전히 변경되었습니다. 그러나 다음과 같이 적절한 결과를 얻었습니다.
1.9.3p194 :008 > products = Product.order_by([:_id, :asc ]).limit(5)
=> #<Mongoid::Criteria
selector: {},
options: {:sort=>{"_id"=>1}, :limit=>5},
class: Product,
embedded: false>
1.9.3p194 :028 > products[0].id
=> "500fa5614f6d3a23d0000002"
1.9.3p194 :029 > products[-1].id
=> "500fa5614f6d3a23d0000006"
이것은 몽고 이드 3.0.0과 관련이있는 것 같습니다. 어떤 아이디어?
은 최고 사용! 감사. 카운트 (true)가 작동하지 않습니다. 'p.count (true)'는 true의 경우 NoMethodError : 정의되지 않은 메소드'id '를 던집니다 : TrueClass \t from (mongoid-3.0.3) /lib/mongoid/contextual/mongo.rb:70:in count' –