本文整理匯總了Java中com.mongodb.client.model.UpdateOptions類的典型用法代碼示例。如果您正苦於以下問題:Java UpdateOptions類的具體用法?Java UpdateOptions怎麽用?Java UpdateOptions使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
UpdateOptions類屬於com.mongodb.client.model包,在下文中一共展示了UpdateOptions類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: update
import com.mongodb.client.model.UpdateOptions; //導入依賴的package包/類
@Override
public ActivityCategory update(ActivityCategory activityCategory) throws DataNotFoundException {
BasicDBObject query = new BasicDBObject();
query.put("objectType", "ActivityCategory");
query.put("name", activityCategory.getName());
UpdateResult updateResult = collection.updateOne(query,
new Document("$set", new Document("label",activityCategory.getLabel())), new UpdateOptions().upsert(false));
if (updateResult.getMatchedCount() == 0){
throw new DataNotFoundException(
new Throwable("ActivityCategory {" + activityCategory.getName() + "} not found."));
}
return activityCategory;
}
示例2: setNewDefault
import com.mongodb.client.model.UpdateOptions; //導入依賴的package包/類
@Override
public void setNewDefault(String name) throws DataNotFoundException {
BasicDBObject query = new BasicDBObject();
query.put("objectType", "ActivityCategory");
query.put("isDefault", true);
UpdateResult undefaultResult = collection.updateOne(query, new Document("$set", new Document("isDefault", false)), new UpdateOptions().upsert(false));
if (undefaultResult.getMatchedCount() > 1){
throw new DataNotFoundException(
new Throwable("Default category error. More than one default found"));
}
BasicDBObject otherQuery = new BasicDBObject();
otherQuery.put("objectType", "ActivityCategory");
otherQuery.put("name", name);
UpdateResult defaultSetResult = collection.updateOne(otherQuery, new Document("$set", new Document("isDefault", true)), new UpdateOptions().upsert(false));
if (defaultSetResult.getMatchedCount() == 0){
throw new DataNotFoundException(
new Throwable("ActivityCategory {" + name + "} not found."));
}
}
示例3: doUpdate
import com.mongodb.client.model.UpdateOptions; //導入依賴的package包/類
protected final FluentFuture<Integer> doUpdate(
final Constraints.ConstraintHost criteria,
final Constraints.Constraint update,
final UpdateOptions options) {
checkNotNull(criteria, "criteria");
checkNotNull(update, "update");
checkNotNull(options, "options");
return submit(new Callable<UpdateResult>() {
@Override
public UpdateResult call() {
return collection()
.updateMany(
convertToBson(criteria),
convertToBson(update),
options);
}
}).lazyTransform(new Function<UpdateResult, Integer>() {
@Override
public Integer apply(UpdateResult input) {
return (int) input.getModifiedCount();
}
});
}
示例4: doUpsert
import com.mongodb.client.model.UpdateOptions; //導入依賴的package包/類
protected final FluentFuture<Integer> doUpsert(
final Constraints.ConstraintHost criteria,
final T document) {
checkNotNull(criteria, "criteria");
checkNotNull(document, "document");
return submit(new Callable<Integer>() {
@Override
public Integer call() {
collection().replaceOne(convertToBson(criteria), document, new UpdateOptions().upsert(true));
// upsert will always return 1:
// if document doesn't exists, it will be inserted (modCount == 1)
// if document exists, it will be updated (modCount == 1)
return 1;
}
});
}
示例5: upsert
import com.mongodb.client.model.UpdateOptions; //導入依賴的package包/類
/**
* 新增或者更新
*
* @param collectionName 集合名
* @param query 查詢條件
* @param descData 目標數據
* @return
*/
public boolean upsert(String collectionName, MongodbQuery query, Map<String, Object> descData) {
MongoCollection collection = sMongoDatabase.getCollection(collectionName);
UpdateOptions options = new UpdateOptions();
options.upsert(true);
BasicDBObject updateSetValue = new BasicDBObject("$set", descData);
UpdateResult updateResult = collection.updateMany(query.getQuery(), updateSetValue, options);
return updateResult.getUpsertedId() != null ||
(updateResult.getMatchedCount() > 0 && updateResult.getModifiedCount() > 0);
}
示例6: updateTubeCollectionData
import com.mongodb.client.model.UpdateOptions; //導入依賴的package包/類
@Override
public Tube updateTubeCollectionData(long rn,Tube tube) throws DataNotFoundException {
Document parsedCollectionData = Document.parse(TubeCollectionData.serialize(tube.getTubeCollectionData()));
UpdateResult updateLabData = collection.updateOne(and(eq("recruitmentNumber", rn),
eq("tubes.code",tube.getCode())),
set("tubes.$.tubeCollectionData", parsedCollectionData),
new UpdateOptions().upsert(false));
if (updateLabData.getMatchedCount() == 0) {
throw new DataNotFoundException(new Throwable("Laboratory of Participant recruitment number: " + rn
+ " does not exists."));
}
return tube;
}
示例7: updateMany
import com.mongodb.client.model.UpdateOptions; //導入依賴的package包/類
@Override
public UpdateResult updateMany(Bson filter, Bson arg1, UpdateOptions arg2)
{
int writeSize = 0;
OperationMetric metric = null;
if (MongoLogger.GATHERER.isEnabled())
{
List<String> keyValuePairs = MongoUtilities.getKeyValuePairs(filter);
keyValuePairs.add("update");
keyValuePairs.add(arg1.toString());
String operationName = "Mongo : " + getNamespace().getCollectionName() + " : updateMany : " +
MongoUtilities.filterParameters(filter);
metric = startMetric(operationName, keyValuePairs);
addWriteConcern(metric);
}
UpdateResult retVal = collection.updateMany(filter, arg1, arg2);
insertUpdateResultProperties(metric, retVal);
stopMetric(metric, writeSize);
return retVal;
}
示例8: updateOne
import com.mongodb.client.model.UpdateOptions; //導入依賴的package包/類
@Override
public UpdateResult updateOne(Bson filter, Bson arg1, UpdateOptions arg2)
{
int writeSize = 0;
OperationMetric metric = null;
if (MongoLogger.GATHERER.isEnabled())
{
List<String> keyValuePairs = MongoUtilities.getKeyValuePairs(filter);
keyValuePairs.add("update");
keyValuePairs.add(arg1.toString());
String operationName = "Mongo : " + getNamespace().getCollectionName() + " : updateOne : " +
MongoUtilities.filterParameters(filter);
metric = startMetric(operationName, keyValuePairs);
addWriteConcern(metric);
}
UpdateResult retVal = collection.updateOne(filter, arg1, arg2);
insertUpdateResultProperties(metric, retVal);
stopMetric(metric, writeSize);
return retVal;
}
示例9: storeConversationMemorySnapshot
import com.mongodb.client.model.UpdateOptions; //導入依賴的package包/類
@Override
public String storeConversationMemorySnapshot(ConversationMemorySnapshot snapshot) throws IResourceStore.ResourceStoreException {
try {
String json = documentBuilder.toString(snapshot);
Document document = Document.parse(json);
document.remove("id");
if (snapshot.getId() != null) {
document.put("_id", new ObjectId(snapshot.getId()));
conversationCollection.updateOne(new Document("_id", new ObjectId(snapshot.getId())),
new Document("$set", document),
new UpdateOptions().upsert(true));
} else {
conversationCollection.insertOne(document);
}
return document.get("_id").toString();
} catch (IOException e) {
throw new IResourceStore.ResourceStoreException(e.getLocalizedMessage(), e);
}
}
示例10: createDoSave
import com.mongodb.client.model.UpdateOptions; //導入依賴的package包/類
private Function<Exchange, Object> createDoSave() {
return exchange1 -> {
try {
MongoCollection<BasicDBObject> dbCol = calculateCollection(exchange1);
BasicDBObject saveObj = exchange1.getIn().getMandatoryBody(BasicDBObject.class);
UpdateOptions options = new UpdateOptions().upsert(true);
BasicDBObject queryObject = new BasicDBObject("_id", saveObj.get("_id"));
UpdateResult result = dbCol.replaceOne(queryObject, saveObj, options);
exchange1.getIn().setHeader(MongoDbConstants.OID, saveObj.get("_id"));
return result;
} catch (InvalidPayloadException e) {
throw new CamelMongoDbException("Body incorrect type for save", e);
}
};
}
示例11: replace
import com.mongodb.client.model.UpdateOptions; //導入依賴的package包/類
@Override
public void replace(T entity) {
StopWatch watch = new StopWatch();
Object id = null;
validator.validate(entity);
try {
id = mongo.codecs.id(entity);
if (id == null) throw Exceptions.error("entity must have id, entityClass={}", entityClass.getCanonicalName());
collection().replaceOne(Filters.eq("_id", id), entity, new UpdateOptions().upsert(true));
} finally {
long elapsedTime = watch.elapsedTime();
ActionLogContext.track("mongoDB", elapsedTime, 0, 1);
logger.debug("replace, collection={}, id={}, elapsedTime={}", collectionName, id, elapsedTime);
checkSlowOperation(elapsedTime);
}
}
示例12: onRequestEnd
import com.mongodb.client.model.UpdateOptions; //導入依賴的package包/類
/**
* Need to page the session out to Mongo
* @param session
*/
public void onRequestEnd(cfSession Session) {
cfSessionData sessData = getSessionData( Session );
if ( sessData == null )
return;
try{
Document keys = new Document("_id", appName + sessData.getStorageID() );
Document vals = new Document("et", new Date(System.currentTimeMillis() + sessData.getTimeOut() ) );
// Serialize the object
ByteArrayOutputStreamRaw bos = new ByteArrayOutputStreamRaw( 32000 );
FileUtil.saveClass(bos, sessData, true);
byte[] buf = bos.toByteArray();
// Has it really changed; we only update the last used date if it has
if ( !MD5.getDigest(buf).equals( sessData.getMD5() ) )
vals.append("d", buf);
col.updateOne( keys, new Document("$set", vals), new UpdateOptions().upsert(true) );
}catch(Exception e){
cfEngine.log( appName + "MongoDBException: " + e );
}
}
示例13: removeProperty
import com.mongodb.client.model.UpdateOptions; //導入依賴的package包/類
/**
* Un-assigns a key/value property from the element. The object value of the
* removed property is returned.
*
* @param key
* the key of the property to remove from the element
* @return the object value associated with that key prior to removal. Should be
* instance of BsonValue
*/
@Override
public <T> T removeProperty(final String key) {
try {
BsonValue value = getProperty(key);
BsonDocument filter = new BsonDocument();
filter.put(Tokens.ID, new BsonString(this.id));
BsonDocument update = new BsonDocument();
update.put("$unset", new BsonDocument(key, new BsonNull()));
if (this instanceof ChronoVertex) {
graph.getVertexCollection().updateOne(filter, update, new UpdateOptions().upsert(true));
return (T) value;
} else {
graph.getEdgeCollection().updateOne(filter, update, new UpdateOptions().upsert(true));
return (T) value;
}
} catch (MongoWriteException e) {
throw e;
}
}
示例14: insertContextDataAggregatedForResoultion
import com.mongodb.client.model.UpdateOptions; //導入依賴的package包/類
private void insertContextDataAggregatedForResoultion(String dbName, String collectionName,
GregorianCalendar calendar, String entityId, String entityType, String attrName, String attrType,
double max, double min, double sum, double sum2, int numSamples, Resolution resolution) {
// Get database and collection
MongoDatabase db = getDatabase(dbName);
MongoCollection collection = db.getCollection(collectionName);
// Build the query
BasicDBObject query = buildQueryForInsertAggregated(calendar, entityId, entityType, attrName, resolution);
// Prepopulate if needed
BasicDBObject insert = buildInsertForPrepopulate(attrType, resolution, true);
UpdateResult res = collection.updateOne(query, insert, new UpdateOptions().upsert(true));
if (res.getMatchedCount() == 0) {
LOGGER.debug("Prepopulating data, database=" + dbName + ", collection=" + collectionName + ", query="
+ query.toString() + ", insert=" + insert.toString());
} // if
// Do the update
BasicDBObject update = buildUpdateForUpdate(attrType, calendar, max, min, sum, sum2, numSamples);
LOGGER.debug("Updating data, database=" + dbName + ", collection=" + collectionName + ", query="
+ query.toString() + ", update=" + update.toString());
collection.updateOne(query, update);
}
示例15: storeBlock
import com.mongodb.client.model.UpdateOptions; //導入依賴的package包/類
public static void storeBlock(MongoBlock mongoBlock) {
// System.out.println("Store: " + mongoBlock.getBlockNumber());
MongoCollection<Document> c = mongoBlock.mongoFile.mongoDirectory.getBlocksCollection();
Document query = new Document();
query.put(MongoDirectory.FILE_NUMBER, mongoBlock.mongoFile.fileNumber);
query.put(MongoDirectory.BLOCK_NUMBER, mongoBlock.blockNumber);
Document object = new Document();
object.put(MongoDirectory.FILE_NUMBER, mongoBlock.mongoFile.fileNumber);
object.put(MongoDirectory.BLOCK_NUMBER, mongoBlock.blockNumber);
object.put(MongoDirectory.BYTES, new Binary(mongoBlock.bytes));
c.replaceOne(query, object, new UpdateOptions().upsert(true));
}