当前位置: 首页>>代码示例>>Java>>正文


Java UpdateResult类代码示例

本文整理汇总了Java中com.mongodb.client.result.UpdateResult的典型用法代码示例。如果您正苦于以下问题:Java UpdateResult类的具体用法?Java UpdateResult怎么用?Java UpdateResult使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


UpdateResult类属于com.mongodb.client.result包,在下文中一共展示了UpdateResult类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: updateDocumentWithCurrentDate

import com.mongodb.client.result.UpdateResult; //导入依赖的package包/类
/**
 * This method update document with lastmodified properties 
 */
@Override
public void updateDocumentWithCurrentDate() {
	MongoDatabase db = null;
	MongoCollection collection = null;
	Bson filter = null;
	Bson query = null;
	try {
		db = client.getDatabase(mongo.getDataBase());
		collection = db.getCollection(mongo.getSampleCollection());
		filter = eq("name", "Sundar");
		query = combine(set("age", 23), set("gender", "Male"),
				currentDate("lastModified"));
		UpdateResult result = collection.updateOne(filter, query);
		log.info("Update with date Status : " + result.wasAcknowledged());
		log.info("No of Record Modified : " + result.getModifiedCount());
	} catch (MongoException e) {
		log.error("Exception occurred while update Many Document with Date : " + e, e);
	}
}
 
开发者ID:sundarcse1216,项目名称:mongodb-crud,代码行数:23,代码来源:UpdateDocumentsImpl.java

示例2: update

import com.mongodb.client.result.UpdateResult; //导入依赖的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;
}
 
开发者ID:ccem-dev,项目名称:otus-api,代码行数:17,代码来源:ActivityConfigurationDaoBean.java

示例3: setNewDefault

import com.mongodb.client.result.UpdateResult; //导入依赖的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."));
    }

}
 
开发者ID:ccem-dev,项目名称:otus-api,代码行数:24,代码来源:ActivityConfigurationDaoBean.java

示例4: doUpdate

import com.mongodb.client.result.UpdateResult; //导入依赖的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();
    }
  });
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:26,代码来源:Repositories.java

示例5: updateManyDocument

import com.mongodb.client.result.UpdateResult; //导入依赖的package包/类
/**
 * This method update all the matches document
 */
@Override
public void updateManyDocument() {
	MongoDatabase db = null;
	MongoCollection collection = null;
	Bson filter = null;
	Bson query = null;
	try {
		db = client.getDatabase(mongo.getDataBase());
		collection = db.getCollection(mongo.getSampleCollection());
		filter = eq("name", "Sundar");
		query = combine(set("age", 23), set("gender", "Male"));
		UpdateResult result = collection.updateMany(filter, query);
		log.info("UpdateMany Status : " + result.wasAcknowledged());
		log.info("No of Record Modified : " + result.getModifiedCount());
	} catch (MongoException e) {
		log.error("Exception occurred while update Many Document : " + e, e);
	}
}
 
开发者ID:sundarcse1216,项目名称:mongodb-crud,代码行数:22,代码来源:UpdateDocumentsImpl.java

示例6: upsert

import com.mongodb.client.result.UpdateResult; //导入依赖的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);
}
 
开发者ID:wxz1211,项目名称:dooo,代码行数:18,代码来源:MongodbDataAccess.java

示例7: updateById

import com.mongodb.client.result.UpdateResult; //导入依赖的package包/类
/**
 * 修改记录
 * 
 * @param collectionName
 *            表名
 * @param mongoObj
 *            对象
 * @return
 */
public static boolean updateById(String collectionName, MongoObj mongoObj) {
	MongoCollection<Document> collection = getCollection(collectionName);
	try {
		Bson filter = Filters.eq(MongoConfig.MONGO_ID, mongoObj.getDocument().getObjectId(MongoConfig.MONGO_ID));
		mongoObj.setDocument(null);
		Document document = objectToDocument(mongoObj);
		UpdateResult result = collection.updateOne(filter, new Document(MongoConfig.$SET, document));
		if (result.getMatchedCount() == 1) {
			return true;
		} else {
			return false;
		}
	} catch (Exception e) {
		if (log != null) {
			log.error("修改记录失败", e);
		}
		return false;
	}

}
 
开发者ID:dianbaer,项目名称:grain,代码行数:30,代码来源:MongodbManager.java

示例8: updateTubeCollectionData

import com.mongodb.client.result.UpdateResult; //导入依赖的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;
}
 
开发者ID:ccem-dev,项目名称:otus-api,代码行数:17,代码来源:ParticipantLaboratoryDaoBean.java

示例9: updateMany

import com.mongodb.client.result.UpdateResult; //导入依赖的package包/类
@Override
public UpdateResult updateMany(Bson filter, Bson arg1)
{
    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);

    insertUpdateResultProperties(metric, retVal);

    stopMetric(metric, writeSize);

    return retVal;
}
 
开发者ID:dd00f,项目名称:ibm-performance-monitor,代码行数:25,代码来源:ProfiledMongoCollection.java

示例10: updateOne

import com.mongodb.client.result.UpdateResult; //导入依赖的package包/类
@Override
public UpdateResult updateOne(Bson filter, Bson arg1)
{
    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);

    insertUpdateResultProperties(metric, retVal);

    stopMetric(metric, writeSize);

    return retVal;
}
 
开发者ID:dd00f,项目名称:ibm-performance-monitor,代码行数:25,代码来源:ProfiledMongoCollection.java

示例11: update

import com.mongodb.client.result.UpdateResult; //导入依赖的package包/类
@Override
public T update(T entry) {
    if (entry == null) {
        return null;
    }

    try {
        String entryToJson = this.jsonConverter.toJson(entry);
        Document document = Document.parse(entryToJson);

        UpdateResult updateResult = this.collection.replaceOne(eq("id", document.get("id")), document);

        if (updateResult.getMatchedCount() == 1) { // means one record updated
            return entry;
        }

        return null; //either none or many records updated, so consider the operation not successful.
    } catch (RuntimeException e) {
        LOGGER.error(e);
        return null;
    }
}
 
开发者ID:rndsolutions,项目名称:hawkcd,代码行数:23,代码来源:MongoDbRepository.java

示例12: execute

import com.mongodb.client.result.UpdateResult; //导入依赖的package包/类
@Override
public void execute(final MongoDatabase connection, final Document data) {
    for (final String collectionName : data.keySet()) {
        final MongoCollection<Document> collection = connection.getCollection(collectionName);

        @SuppressWarnings("unchecked")
        final List<Document> documents = data.get(collectionName, List.class);

        for (final Document doc : documents) {
            final UpdateResult result = collection.replaceOne(Filters.eq(doc.get("_id")), doc);

            if (result.getMatchedCount() == 0) {
                collection.insertOne(doc);
            }
        }
    }
}
 
开发者ID:dadrus,项目名称:jpa-unit,代码行数:18,代码来源:RefreshOperation.java

示例13: createDoSave

import com.mongodb.client.result.UpdateResult; //导入依赖的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);
        }
    };
}
 
开发者ID:HydAu,项目名称:Camel,代码行数:17,代码来源:MongoDbProducer.java

示例14: testSave

import com.mongodb.client.result.UpdateResult; //导入依赖的package包/类
@Test
public void testSave() throws Exception {
    // Prepare test
    assertEquals(0, testCollection.count());
    Object[] req = new Object[] {"{\"_id\":\"testSave1\", \"scientist\":\"Einstein\"}", "{\"_id\":\"testSave2\", \"scientist\":\"Copernicus\"}"};
    Object result = template.requestBody("direct:insert", req);
    assertTrue(result instanceof List);
    assertEquals("Number of records persisted must be 2", 2, testCollection.count());
    
    // Testing the save logic
    DBObject record1 = testCollection.find(new BasicDBObject("_id", "testSave1")).first();
    assertEquals("Scientist field of 'testSave1' must equal 'Einstein'", "Einstein", record1.get("scientist"));
    record1.put("scientist", "Darwin");
    
    result = template.requestBody("direct:save", record1);
    assertTrue(result instanceof UpdateResult);
    
    record1 = testCollection.find(new BasicDBObject("_id", "testSave1")).first();
    assertEquals("Scientist field of 'testSave1' must equal 'Darwin' after save operation", "Darwin", record1.get("scientist"));

}
 
开发者ID:HydAu,项目名称:Camel,代码行数:22,代码来源:MongoDbOperationsTest.java

示例15: updateZForSection

import com.mongodb.client.result.UpdateResult; //导入依赖的package包/类
public void updateZForSection(final StackId stackId,
                              final String sectionId,
                              final Double z)
        throws IllegalArgumentException, IllegalStateException {

    MongoUtil.validateRequiredParameter("stackId", stackId);
    MongoUtil.validateRequiredParameter("sectionId", sectionId);
    MongoUtil.validateRequiredParameter("z", z);

    final MongoCollection<Document> tileCollection = getTileCollection(stackId);
    final Document query = new Document("layout.sectionId", sectionId);
    final Document update = new Document("$set", new Document("z", z));

    final UpdateResult result = tileCollection.updateMany(query, update);

    LOG.debug("updateZForSection: updated {} tile specs with {}.update({},{})",
              result.getModifiedCount(), MongoUtil.fullName(tileCollection), query.toJson(), update.toJson());
}
 
开发者ID:saalfeldlab,项目名称:render,代码行数:19,代码来源:RenderDao.java


注:本文中的com.mongodb.client.result.UpdateResult类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。