{"record":{"id":"9eaa403f46d3ba44","repo":"mongodb/mongoid","slug":"cannot-specify-set-on-insert-with-replace-true","errorCode":null,"errorMessage":"cannot specify :set_on_insert with `replace: true`","messagePattern":"cannot specify :set_on_insert with `replace: true`","errorType":"exception","errorClass":"ArgumentError","httpStatus":null,"severity":"error","filePath":"lib/mongoid/persistable/upsertable.rb","lineNumber":36,"sourceCode":"      # @example Upsert the document with replace.\n      #   document.upsert(replace: true)\n      #\n      # @example Upsert with extra attributes to use when inserting.\n      #   document.upsert(set_on_insert: { created_at: DateTime.now })\n      #\n      # @param [ Hash ] options The validation options.\n      #\n      # @option options [ true | false ] :validate Whether or not to validate.\n      # @option options [ true | false ] :replace Whether or not to replace\n      #   the document on upsert.\n      # @option options [ Hash ] :set_on_insert The attributes to include if\n      #   the document does not already exist.\n      #\n      # @return [ true ] True.\n      def upsert(options = {})\n        prepare_upsert(options) do\n          if options[:replace]\n            raise ArgumentError, 'cannot specify :set_on_insert with `replace: true`' if options[:set_on_insert]\n\n            collection.find(atomic_selector).replace_one(\n              as_attributes, upsert: true, session: _session\n            )\n          else\n            attrs = { '$set' => as_attributes }\n            attrs['$setOnInsert'] = options[:set_on_insert] if options[:set_on_insert]\n\n            collection.find(atomic_selector).update_one(\n              attrs, upsert: true, session: _session\n            )\n          end\n        end\n      end\n\n      private\n\n      # Prepare the upsert for execution.","sourceCodeStart":18,"sourceCodeEnd":54,"githubUrl":"https://github.com/mongodb/mongoid/blob/0da0e23d71adfbad31128c18e5e2c8c301c6f7c4/lib/mongoid/persistable/upsertable.rb#L18-L54","documentation":"Error \"cannot specify :set_on_insert with `replace: true`\" thrown in mongodb/mongoid.","triggerScenarios":"Thrown at lib/mongoid/persistable/upsertable.rb:36 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":[],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"0da0e23d71adfbad31128c18e5e2c8c301c6f7c4","analyzedAt":"2026-08-23T06:25:47.656Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}