Example usage for com.mongodb WriteResult isUpdateOfExisting

List of usage examples for com.mongodb WriteResult isUpdateOfExisting

Introduction

In this page you can find the example usage for com.mongodb WriteResult isUpdateOfExisting.

Prototype

public boolean isUpdateOfExisting() 

Source Link

Document

Returns true if this write resulted in an update of an existing document.

Usage

From source file:org.nmdp.hmlfhirconvertermodels.domain.base.CascadingUpdate.java

License:Open Source License

private IMongoDataRepositoryModel upsert(IMongoDataRepositoryModel model, MongoOperations mongoOperations,
        String collectionName) throws Exception {
    WriteResult writeResult = mongoOperations.upsert(buildUpsertQuery(model), buildUpsertUpdate(model),
            collectionName);//  w  ww  .  j  a  v  a 2 s .  c o  m
    DBCollection collection = mongoOperations.getCollection(collectionName);
    Object upsertedId;

    if (writeResult.isUpdateOfExisting()) {
        try {
            Field field = model.getClass().getDeclaredField("id");
            field.setAccessible(true);
            String id = field.get(model).toString();
            upsertedId = objectifyId(id);
        } catch (Exception ex) {
            LOG.error(ex);
            throw new Exception("Unable to evaluate 'id' property", ex);
        }
    } else {
        upsertedId = writeResult.getUpsertedId();
    }

    DBObject result = collection.findOne(upsertedId);
    return model.convertGenericResultToModel(result, model, getDocumentProperties(model));
}

From source file:watchdog.core.VersionController.java

License:Apache License

public static void commit(Model currentModel) {
    try {//  w ww .  j a v  a2  s .  co m
        MongoClient client = new MongoClient();
        DB db = client.getDB(DB_NAME);

        DBCollection tripleStore = getTripleStore(db);
        List<Statement> statements = currentModel.listStatements().toList();
        int failureCount = 0;

        for (Statement stmt : statements) {
            String subj = stmt.getSubject().toString();
            String pre = stmt.getPredicate().toString();
            String obj = stmt.getObject().toString();

            BasicDBObject record = new BasicDBObject("sub", subj).append("pre", pre).append("obj", obj)
                    .append("grade", new BasicDBList()).append("avgGrade", 0.0d);

            try {
                WriteResult insertResult = tripleStore.insert(record);
                if (!insertResult.isUpdateOfExisting()) {
                    failureCount++;
                }
            } catch (MongoException ex) {
                ex.printStackTrace();
                failureCount++;
            }
        }

        LogUtil.L.info(failureCount + "/" + statements.size() + " triples successfully added");
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }
}