Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-1248

Model#first and Model#last returning same record when collection has multiple documents

    • Type: Icon: Task Task
    • Resolution: Done
    • 2.2.1
    • Affects Version/s: None
    • Component/s: None

      This has been discussed before here in https://github.com/mongoid/mongoid/issues/1080, but I don't have permission to reopen it.

      This is occasionally happening to me, however I don't think it is a Mongoid issue alone, as the behavior can be observed when falling back to the driver. I can't isolate the cause. It's intermittent it doesn't affect all collections simultaneously. Should I post this on mongo-ruby-driver issues or it's relevant to have it here as well?

      I'm using:

      • mongoid/master
      • mongodb 1.8.2
      • mongo-ruby-driver 1.3.1
      # Collection with more than one record
      User.count
      2
      # User#first and User#last returning same record
      User.first.id
      BSON::ObjectId('4e70407c9788eb1086000007')
      
      User.last.id
      BSON::ObjectId('4e70407c9788eb1086000007')
      
      # Retrieving last document from the driver. 
      # There aren't any custom indexes defined for this collection.
      User.collection.driver.find().sort([['_id',-1]]).limit(1).first._id
      BSON::ObjectId('4e70407c9788eb1086000007')
      
      # Omitting #sort will return last document
      User.collection.driver.find().limit(1).first._id
      BSON::ObjectId('4e70407c9788eb1086000007')
      
      # Forcing #sort to asc _id returns actual first document
      User.collection.driver.find().sort([['_id',1]]).limit(1).first._id
      BSON::ObjectId('4e7044e49788eb108600000c')
      
      

            Assignee:
            Unassigned Unassigned
            Reporter:
            angelim angelim
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: