Method: Mongo::Collection#replace_one
- Defined in:
- lib/mongo/collection.rb
#replace_one(filter, replacement, options = {}) ⇒ Result
Replaces a single document in the collection with the new document.
Examples:
Replace a single document.
Replace a single document.
collection.replace_one({ name: 'test' }, { name: 'test1' })
Parameters:
-
filter
(Hash)
—
The filter to use.
-
replacement
(Hash)
—
The replacement document..
-
options
(Hash)
(defaults to: {})
—
The options.
Options Hash (options):
-
:upsert
(true | false)
—
Whether to upsert if the document doesn’t exist.
-
:bypass_document_validation
(true | false)
—
Whether or not to skip document level validation.
-
:collation
(Hash)
—
The collation to use.
-
:session
(Session)
—
The session to use.
-
:timeout_ms
(Integer)
—
The operation timeout in milliseconds. Must be a non-negative integer. An explicit value of 0 means infinite. The default value is unset which means the value is inherited from the collection or the database or the client.
-
:hint
(Hash | String)
—
The index to use for this operation. May be specified as a Hash (e.g. { _id: 1 }) or a String (e.g. “id”).
-
:let
(Hash)
—
Mapping of variables to use in the command. See the server documentation for details.
-
:sort
(Hash)
—
Specifies which document the operation replaces if the query matches multiple documents. The first document matched by the sort order will be replaced. This option is only supported by servers >= 8.0. Older servers will report an error for using this option.
Returns:
-
(Result)
—
The response from the database.
Since:
-
2.1.0
1071 1072 1073 |
# File 'lib/mongo/collection.rb', line 1071 def replace_one(filter, replacement, options = {}) find(filter, options).replace_one(replacement, options) end |