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

reload fails to find document when the document's sharded field has been updated

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 7.2.2, 7.1.8, 7.0.13
    • Affects Version/s: 7.2.1
    • Component/s: Query
    • None
    • Fully Compatible

      Mongoid fails to reload documents with sharded fields that have been modified. The simple repro below uses Mongoid's Profile test model to exhibit this behavior.

      Repro: (create, modify shard key, save, reload, BROKEN)

      let(:profile) do
        # Profile shard_key :name
        Profile.create(name: "Alice")
      end
      
      it "successfully reloads the document after saving an update to the sharded field" do
        expect(profile.name).to eq("Alice")
        profile.name = "Bob"
        profile.save()
      
        # Fails
        profile.reload()
      
        expect(profile.name).to eq("Bob")
      end
      

      The line marked as # Fails above fails with the following error:

      Failure/Error: raise Errors::DocumentNotFound.new(self.class, _id, _id)
           
       Mongoid::Errors::DocumentNotFound:
       
         message:
           Document(s) not found for class Profile with id(s) 606fabb4235bf0215ae36b7f.
         summary:
           When calling Profile.find with an id or array of ids, each parameter must match a document in the database or this error will be raised. The search was for the id(s): 606fabb4235bf0215ae36b7f (1 total) and the following ids were not found: 606fabb4235bf0215ae36b7f.
         resolution:
           Search for an id that is in the database or set the Mongoid.raise_not_found_error configuration option to false, which will cause a nil to be returned instead of raising this error when searching for a single id, or only the matched documents when searching for multiples.
      

      The failure occurs because atomic_selector at the time of the call is set to:

      {{"_id"=>BSON::ObjectId('606fabb4235bf0215ae36b7f'), "name"=>"Alice"}}
      

            Assignee:
            oleg.pudeyev@mongodb.com Oleg Pudeyev (Inactive)
            Reporter:
            dalton.black@braze.com Dalton Black
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: