com.mongodb.internal.operation.FindAndReplaceOperation Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mongodb-driver-core Show documentation
Show all versions of mongodb-driver-core Show documentation
The Java operations layer for the MongoDB Java Driver.
Third parties can wrap this layer to provide custom higher-level APIs
/*
* Copyright 2008-present MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mongodb.internal.operation;
import com.mongodb.MongoNamespace;
import com.mongodb.WriteConcern;
import com.mongodb.client.model.Collation;
import com.mongodb.connection.ConnectionDescription;
import com.mongodb.internal.validator.MappedFieldNameValidator;
import com.mongodb.internal.validator.NoOpFieldNameValidator;
import com.mongodb.internal.validator.ReplacingDocumentFieldNameValidator;
import com.mongodb.lang.Nullable;
import org.bson.BsonBoolean;
import org.bson.BsonDocument;
import org.bson.BsonValue;
import org.bson.FieldNameValidator;
import org.bson.codecs.Decoder;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import static com.mongodb.assertions.Assertions.notNull;
import static com.mongodb.internal.operation.DocumentHelper.putIfTrue;
/**
* An operation that atomically finds and replaces a single document.
*
* This class is not part of the public API and may be removed or changed at any time
*/
public class FindAndReplaceOperation extends BaseFindAndModifyOperation {
private final BsonDocument replacement;
private boolean returnOriginal = true;
private boolean upsert;
private Boolean bypassDocumentValidation;
public FindAndReplaceOperation(final MongoNamespace namespace, final WriteConcern writeConcern, final boolean retryWrites,
final Decoder decoder, final BsonDocument replacement) {
super(namespace, writeConcern, retryWrites, decoder);
this.replacement = notNull("replacement", replacement);
}
public BsonDocument getReplacement() {
return replacement;
}
public boolean isReturnOriginal() {
return returnOriginal;
}
public FindAndReplaceOperation returnOriginal(final boolean returnOriginal) {
this.returnOriginal = returnOriginal;
return this;
}
public boolean isUpsert() {
return upsert;
}
public FindAndReplaceOperation upsert(final boolean upsert) {
this.upsert = upsert;
return this;
}
public Boolean getBypassDocumentValidation() {
return bypassDocumentValidation;
}
public FindAndReplaceOperation bypassDocumentValidation(@Nullable final Boolean bypassDocumentValidation) {
this.bypassDocumentValidation = bypassDocumentValidation;
return this;
}
@Override
public FindAndReplaceOperation filter(@Nullable final BsonDocument filter) {
super.filter(filter);
return this;
}
@Override
public FindAndReplaceOperation projection(@Nullable final BsonDocument projection) {
super.projection(projection);
return this;
}
@Override
public FindAndReplaceOperation maxTime(final long maxTime, final TimeUnit timeUnit) {
super.maxTime(maxTime, timeUnit);
return this;
}
@Override
public FindAndReplaceOperation sort(@Nullable final BsonDocument sort) {
super.sort(sort);
return this;
}
@Override
public FindAndReplaceOperation hint(@Nullable final BsonDocument hint) {
super.hint(hint);
return this;
}
@Override
public FindAndReplaceOperation hintString(@Nullable final String hint) {
super.hintString(hint);
return this;
}
@Override
public FindAndReplaceOperation collation(@Nullable final Collation collation) {
super.collation(collation);
return this;
}
@Override
public FindAndReplaceOperation comment(@Nullable final BsonValue comment) {
super.comment(comment);
return this;
}
@Override
public FindAndReplaceOperation let(@Nullable final BsonDocument variables) {
super.let(variables);
return this;
}
protected FieldNameValidator getFieldNameValidator() {
Map map = new HashMap<>();
map.put("update", new ReplacingDocumentFieldNameValidator());
return new MappedFieldNameValidator(new NoOpFieldNameValidator(), map);
}
protected void specializeCommand(final BsonDocument commandDocument, final ConnectionDescription connectionDescription) {
commandDocument.put("new", new BsonBoolean(!isReturnOriginal()));
putIfTrue(commandDocument, "upsert", isUpsert());
commandDocument.put("update", getReplacement());
if (bypassDocumentValidation != null) {
commandDocument.put("bypassDocumentValidation", BsonBoolean.valueOf(bypassDocumentValidation));
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy