本文整理汇总了Java中com.mongodb.client.result.UpdateResult.getMatchedCount方法的典型用法代码示例。如果您正苦于以下问题:Java UpdateResult.getMatchedCount方法的具体用法?Java UpdateResult.getMatchedCount怎么用?Java UpdateResult.getMatchedCount使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.mongodb.client.result.UpdateResult
的用法示例。
在下文中一共展示了UpdateResult.getMatchedCount方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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;
}
示例2: 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."));
}
}
示例3: 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);
}
示例4: 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;
}
}
示例5: 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;
}
示例6: 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;
}
}
示例7: 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);
}
}
}
}
示例8: insertContextDataAggregatedForResoultion
import com.mongodb.client.result.UpdateResult; //导入方法依赖的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);
}
示例9: save
import com.mongodb.client.result.UpdateResult; //导入方法依赖的package包/类
/**
* stores the given EntityInvocationHandler represented Entity in the given Collection
*
* @param handler EntityInvocationHandler (Entity) to save
* @param coll MongoCollection to save entity into
*/
@SuppressWarnings( "unchecked" )
static <T extends Entity> void save( EntityInvocationHandler handler, MongoCollection<T> coll )
{
for ( ParameterProperty cpp : handler.properties.getValidationProperties() )
{
cpp.validate( handler.data.get( cpp.getMongoName() ) );
}
BsonDocumentWrapper wrapper = new BsonDocumentWrapper<>( handler.proxy,
(org.bson.codecs.Encoder<Entity>) coll.getCodecRegistry().get( handler.properties.getEntityClass() ) );
UpdateResult res = coll.updateOne(
new BsonDocument( "_id",
BsonDocumentWrapper.asBsonDocument( EntityCodec._obtainId( handler.proxy ), idRegistry ) ),
new BsonDocument( "$set", wrapper ), new UpdateOptions() );
if ( res.getMatchedCount() == 0 )
{
// TODO this seems too nasty, there must be a better way.for now live with it
coll.insertOne( (T) handler.proxy );
}
handler.persist();
}
示例10: update
import com.mongodb.client.result.UpdateResult; //导入方法依赖的package包/类
@Override
public SurveyActivity update(SurveyActivity surveyActivity) throws DataNotFoundException {
Document parsed = Document.parse(SurveyActivity.serialize(surveyActivity));
parsed.remove("_id");
UpdateResult updateOne = collection.updateOne(eq("_id", surveyActivity.getActivityID()),
new Document("$set", parsed), new UpdateOptions().upsert(false));
if (updateOne.getMatchedCount() == 0) {
throw new DataNotFoundException(
new Throwable("OID {" + surveyActivity.getActivityID().toString() + "} not found."));
}
return surveyActivity;
}
示例11: disable
import com.mongodb.client.result.UpdateResult; //导入方法依赖的package包/类
@Override
public void disable(String name) throws DataNotFoundException {
BasicDBObject query = new BasicDBObject();
query.put("objectType", "ActivityCategory");
query.put("name", name);
UpdateResult updateResult = collection.updateOne(query, new Document("$set", new Document("disabled", true)), new UpdateOptions().upsert(false));
if (updateResult.getMatchedCount() == 0) {
throw new DataNotFoundException(
new Throwable("ActivityCategory {" + name + "} not found.")); }
}
示例12: update
import com.mongodb.client.result.UpdateResult; //导入方法依赖的package包/类
@Override
public ExamLot update(ExamLot examLot) throws DataNotFoundException {
Document parsed = Document.parse(ExamLot.serialize(examLot));
parsed.remove("_id");
UpdateResult updateLotData = collection.updateOne(eq("code", examLot.getCode()), new Document("$set", parsed),
new UpdateOptions().upsert(false));
if (updateLotData.getMatchedCount() == 0) {
throw new DataNotFoundException(new Throwable("Exam Lot not found"));
}
return examLot;
}
示例13: update
import com.mongodb.client.result.UpdateResult; //导入方法依赖的package包/类
@Override
public TransportationLot update(TransportationLot transportationLot) throws DataNotFoundException {
Document parsed = Document.parse(TransportationLot.serialize(transportationLot));
parsed.remove("_id");
UpdateResult updateLotData = collection.updateOne(eq("code", transportationLot.getCode()),
new Document("$set", parsed), new UpdateOptions().upsert(false));
if (updateLotData.getMatchedCount() == 0) {
throw new DataNotFoundException(
new Throwable("Transportation Lot not found"));
}
return transportationLot;
}
示例14: updateLaboratoryData
import com.mongodb.client.result.UpdateResult; //导入方法依赖的package包/类
@Override
public ParticipantLaboratory updateLaboratoryData(ParticipantLaboratory labParticipant) throws DataNotFoundException {
Document parsed = Document.parse(ParticipantLaboratory.serialize(labParticipant));
parsed.remove("_id");
UpdateResult updateLabData = collection.updateOne(eq("recruitmentNumber", labParticipant.getRecruitmentNumber()), new Document("$set", parsed),
new UpdateOptions().upsert(false));
if (updateLabData.getMatchedCount() == 0) {
throw new DataNotFoundException(new Throwable("Laboratory of Participant recruitment number: " + labParticipant.getRecruitmentNumber()
+ " does not exists."));
}
return labParticipant;
}
示例15: updateOne
import com.mongodb.client.result.UpdateResult; //导入方法依赖的package包/类
@Override
public boolean updateOne(String docName, Document doc, Document docFilter) {
UpdateResult upResult = baseUpdateOne(docName, doc, docFilter);
if (upResult == null) {
return false;
}
long matched = upResult.getMatchedCount();//匹配上的数据条数
long modified = upResult.getModifiedCount();//已修改的数据条数
if (matched == 1 && modified == 1) {
return true;
}
return false;
}