-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for replaceOne operation.
Add replace methods to MongoOperations and MongoTemplate that allow to replace the first matching document with a given value. Closes: #4462 Original Pull Request: #4463
- Loading branch information
1 parent
66c4a3b
commit f7549f7
Showing
6 changed files
with
353 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReplaceOptions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/** | ||
* Options for | ||
* <a href="https://docs.mongodb.com/manual/reference/method/db.collection.replaceOne/">replaceOne</a>. | ||
* <br /> | ||
* Defaults to | ||
* <dl> | ||
* <dt>upsert</dt> | ||
* <dd>false</dd> | ||
* </dl> | ||
* | ||
* @author Jakub Zurawa | ||
*/ | ||
package org.springframework.data.mongodb.core; | ||
|
||
public class ReplaceOptions { | ||
private boolean upsert; | ||
|
||
private static final ReplaceOptions NONE = new ReplaceOptions() { | ||
|
||
private static final String ERROR_MSG = "ReplaceOptions.none() cannot be changed; Please use ReplaceOptions.options() instead"; | ||
|
||
@Override | ||
public ReplaceOptions upsert() { | ||
throw new UnsupportedOperationException(ERROR_MSG); | ||
} | ||
}; | ||
|
||
/** | ||
* Static factory method to create a {@link ReplaceOptions} instance. | ||
* <dl> | ||
* <dt>upsert</dt> | ||
* <dd>false</dd> | ||
* </dl> | ||
* | ||
* @return new instance of {@link ReplaceOptions}. | ||
*/ | ||
public static ReplaceOptions options() { | ||
return new ReplaceOptions(); | ||
} | ||
|
||
/** | ||
* Static factory method returning an unmodifiable {@link ReplaceOptions} instance. | ||
* | ||
* @return unmodifiable {@link ReplaceOptions} instance. | ||
* @since 2.2 | ||
*/ | ||
public static ReplaceOptions none() { | ||
return NONE; | ||
} | ||
|
||
/** | ||
* Static factory method to create a {@link ReplaceOptions} instance with | ||
* <dl> | ||
* <dt>upsert</dt> | ||
* <dd>false</dd> | ||
* </dl> | ||
* | ||
* @return new instance of {@link ReplaceOptions}. | ||
*/ | ||
public static ReplaceOptions empty() { | ||
return new ReplaceOptions(); | ||
} | ||
|
||
/** | ||
* Insert a new document if not exists. | ||
* | ||
* @return this. | ||
*/ | ||
public ReplaceOptions upsert() { | ||
|
||
this.upsert = true; | ||
return this; | ||
} | ||
|
||
/** | ||
* Get the bit indicating if to create a new document if not exists. | ||
* | ||
* @return {@literal true} if set. | ||
*/ | ||
public boolean isUpsert() { | ||
return upsert; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.