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:br.ufabc.impress.mongo.manager.DBHelper.java

@Override
public boolean modify(String tableName, String json, String _id) {
    if (tableName == null || tableName.equals("") || json == null || json.equals("") || _id == null
            || _id.equals("")) {
        return false;
    }/*from   w w  w .  jav  a 2 s .co m*/
    DBCollection table = db.getCollection(tableName);
    BasicDBObject searchQuery = new BasicDBObject();
    searchQuery.put("_id", new ObjectId(_id));
    DBObject dbObject = (DBObject) JSON.parse(json);
    dbObject.put("update", System.currentTimeMillis() + "");
    WriteResult result = table.update(searchQuery, dbObject);
    return result.isUpdateOfExisting();
}

From source file:br.ufabc.impress.mongo.manager.DBHelper.java

@Override
public boolean modifyByCondition(String tableName, String json, Map condition) {
    if (tableName == null || tableName.equals("") || json == null || json.equals("") || condition == null
            || condition.isEmpty()) {//from   www .  ja v  a2s .  co m
        return false;
    }
    DBCollection table = db.getCollection(tableName);
    BasicDBObject searchQuery = new BasicDBObject(condition);
    DBObject dbObject = (DBObject) JSON.parse(json);
    dbObject.put("update", System.currentTimeMillis() + "");
    WriteResult result = table.update(searchQuery, dbObject);
    return result.isUpdateOfExisting();
}

From source file:br.ufabc.impress.mongo.manager.DBHelper.java

@Override
public List<Boolean> modifyNotOveride(String tableName, String json, String _id) {
    if (tableName == null || tableName.equals("") || json == null || json.equals("") || _id == null
            || _id.equals("")) {
        return null;
    }/*from   w ww. jav  a  2  s  . c o  m*/
    List<Boolean> resList = new ArrayList();
    DBCollection table = db.getCollection(tableName);
    DBCursor cursor = table.find();
    while (cursor.hasNext()) {
        DBObject updateDocument = cursor.next();
        DBObject searchQuery = cursor.next();
        DBObject dbObject = (DBObject) JSON.parse(json);
        updateDocument.putAll(dbObject.toMap());
        updateDocument.removeField("_id");
        WriteResult result = table.update(searchQuery, updateDocument);
        resList.add(result.isUpdateOfExisting());
    }
    return resList;
}

From source file:com.nec.strudel.tkvs.store.mongodb.MongodbTransaction.java

License:Apache License

@Override
public boolean commit() {
    prof.commitStart(gName);/*ww  w  . java  2s. co m*/
    BasicDBObject setdoc = new BasicDBObject();
    BasicDBObject unsetdoc = new BasicDBObject();
    for (CollectionBuffer b : buffers()) {
        Map<Key, Record> writes = b.getWrites();
        String name = b.getName();
        for (Map.Entry<Key, Record> e : writes.entrySet()) {
            Key key = e.getKey();
            Record r = e.getValue();
            //for put
            if (r != null) {
                setdoc.append(key.toStringKey(name), r.toBytes());
            } else { // for delete
                unsetdoc.append(key.toStringKey(name), "");
            }
        }
    }
    if (setdoc.isEmpty() && unsetdoc.isEmpty()) {
        //read only
        long newvnum = getVnum(coll, docName);
        if (this.vnum == newvnum) {
            prof.commitSuccess(gName);
            return true;
        } else {
            prof.commitFail(gName);
            return false;
        }
    } else {
        setdoc.append(MongoDbServer.VERSIONFIELD, vnum + 1);
        WriteResult res = null;
        if (vnum == 0) {
            //upsert if this docName is not in the db yet,
            //else we should receive a duplicateKey exception, meaning
            //this docName is inserted by an other transaction, then fail it
            if (unsetdoc.isEmpty()) {
                res = insert(new BasicDBObject(MongoDbServer.DOCNAME, docName),
                        new BasicDBObject("$set", setdoc));
            } else {
                res = insert(new BasicDBObject(MongoDbServer.DOCNAME, docName),
                        new BasicDBObject("$set", setdoc).append("$unset", unsetdoc));
            }
            if (res == null) {
                prof.commitFail(gName);
                return false;
            }
            prof.commitSuccess(gName);
            return true;
        } else {
            if (unsetdoc.isEmpty()) {
                res = update(new BasicDBObject(MongoDbServer.DOCNAME, docName)
                        .append(MongoDbServer.VERSIONFIELD, this.vnum), new BasicDBObject("$set", setdoc));
            } else {
                res = update(new BasicDBObject(MongoDbServer.DOCNAME, docName)
                        .append(MongoDbServer.VERSIONFIELD, this.vnum),
                        new BasicDBObject("$set", setdoc).append("$unset", unsetdoc));
            }
            if (res.isUpdateOfExisting()) {
                prof.commitSuccess(gName);
                return true;
            } else {
                prof.commitFail(gName);
                return false;
            }
        }
    }
}

From source file:com.ott.bookings.auth.form.impl.MongoTockenStore.java

License:Apache License

private Token saveCurrentToken(Token tokenPointer, Token nextTokenPointer) {

    DBObject selectQuery = new BasicDBObject(ID_PROPERTY, nextTokenPointer.getToken());
    selectQuery.put(ACTIVE_PROPERTY, false);

    BasicDBObject updateFields = new BasicDBObject();
    updateFields.put(ACTIVE_PROPERTY, true);
    updateFields.put(NEXT_UPDATE_PROPERTY, nextTokenPointer.getNextUpdate());
    updateFields.put(SECRET_KEY_PROPERTY, nextTokenPointer.getKey());

    DBObject updateQuery = new BasicDBObject();
    updateQuery.put("$set", updateFields);
    WriteResult result = this.collection.update(selectQuery, updateQuery, true, false);

    // update old token
    selectQuery = new BasicDBObject(ID_PROPERTY, tokenPointer.getToken());
    this.collection.update(selectQuery, new BasicDBObject("$set", new BasicDBObject(ACTIVE_PROPERTY, false)));

    if (result.isUpdateOfExisting()) {
        return nextTokenPointer;
    } else {/*from w ww . ja va  2 s .com*/
        return toToken(this.collection.findOne(new BasicDBObject(ID_PROPERTY, nextTokenPointer.getToken())));
    }
}

From source file:com.staticvillage.recommender.indexer.MongoDBIndexer.java

License:Apache License

@Override
public boolean updateBean(Place bean) {
    DBCollection dbCollection = instanceDB.getCollection(collection);
    BasicDBObject item = new BasicDBObject("id", bean.id);

    WriteResult writeResult = dbCollection.update(item, toDBObject(bean));
    return writeResult.isUpdateOfExisting();
}

From source file:eu.operando.core.ose.mongo.RegulationsMongo.java

public boolean updateRegulationD(String regId, PrivacyRegulationInput reg) {
    boolean result = false;
    //reg.setRegId(regId);
    try {/*  www.j av  a  2 s.c  o m*/
        ObjectMapper mapper = new ObjectMapper();
        String jsonInString = mapper.writeValueAsString(reg);
        Object obj = JSON.parse(jsonInString);
        DBObject document = (DBObject) obj;

        BasicDBObject searchQuery = new BasicDBObject().append("regId", regId);
        WriteResult wr = regulationTable.update(searchQuery, document);

        result = wr.isUpdateOfExisting();

    } catch (JsonGenerationException e) {
        e.printStackTrace();
    } catch (JsonMappingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return result;
}

From source file:eu.operando.core.pdb.mongo.UPPMongo.java

public boolean updateUPP(String regId, UserPrivacyPolicy upp) {
    boolean result = false;
    //upp.setUserPolicyID(regId);
    try {//  w  w  w. j  a  v  a  2 s .  c o  m
        ObjectMapper mapper = new ObjectMapper();
        String jsonInString = mapper.writeValueAsString(upp);
        Object obj = JSON.parse(jsonInString);
        DBObject document = (DBObject) obj;

        BasicDBObject searchQuery = new BasicDBObject();
        ;
        try {
            //                searchQuery = new BasicDBObject().append("_id", new ObjectId(regId));
            searchQuery.put("userId", regId);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            return result;
        }

        WriteResult wr = uppTable.update(searchQuery, document);

        result = wr.isUpdateOfExisting();

    } catch (JsonGenerationException e) {
        e.printStackTrace();
    } catch (JsonMappingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return result;
}

From source file:eu.operando.core.udb.UserAccountMongo.java

/**
 * Update the user account information in the object in the collection.
 * @param userId The id of the user// w w w  .  j  a v  a2s . co  m
 * @param userAcc The updated account information.
 * @return Whether the object was updated or not.
 */
public boolean updateUser(String userId, UserAccount userAcc) {
    boolean result = false;
    //upp.setUserPolicyID(regId);
    try {
        ObjectMapper mapper = new ObjectMapper();
        String jsonInString = mapper.writeValueAsString(userAcc);
        Object obj = JSON.parse(jsonInString);
        DBObject document = (DBObject) obj;

        BasicDBObject searchQuery;
        try {
            searchQuery = new BasicDBObject();
            searchQuery.put("userid", userId);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            return result;
        }
        WriteResult wr = userTable.update(searchQuery, document);
        result = wr.isUpdateOfExisting();

    } catch (JsonGenerationException e) {
        result = false;
    } catch (JsonMappingException e) {
        result = false;
    } catch (IOException e) {
        result = false;
    }
    return result;
}

From source file:org.graylog2.indexer.ranges.MongoIndexRangeService.java

License:Open Source License

public boolean markAsMigrated(String index) {
    final DB db = mongoConnection.getDatabase();
    final DBCollection collection = db.getCollection(COLLECTION_NAME);

    final DBObject updatedData = new BasicDBObject("$set", new BasicDBObject(FIELD_MIGRATED, true));
    final DBObject searchQuery = new BasicDBObject("index", index);
    final WriteResult result = collection.update(searchQuery, updatedData);

    return result.isUpdateOfExisting();
}