List of usage examples for com.mongodb WriteResult getUpsertedId
@Nullable
public Object getUpsertedId()
From source file:com.jjorgemoura.hangmanz.model.ZDHangmanGame.java
public void persist() { if (this.dbEngine == null) { return;//from www . j a v a2 s . c o m } //Prepare JSON Document from Entity DBObject docX = this.createDBObject(); //Prepare Collection DBCollection dbCollection = this.dbEngine.getCollection(ZDHangmanGame.DB_COLLECTION_ID); //Save - Write WriteResult dbInsertResult = dbCollection.insert(docX); //Finalise int n = dbInsertResult.getN(); Object upsertedId = dbInsertResult.getUpsertedId(); WriteConcern lastConcernObject = dbInsertResult.getLastConcern(); String s = "ok"; }
From source file:ezbake.locksmith.db.MongoDBService.java
License:Apache License
/** * Inserts a document into a collection/*from w ww . ja v a 2 s . c om*/ * @param collection - collection to insert document into * @param doc - the document to be inserted * @return - true on success, false otherwise */ public ObjectId insertDocumentIntoCollection(String collection, DBObject doc) { boolean success = true; log.info("Insert Document into collection [{}]", collection); DBCollection coll = db.getCollection(collection); WriteResult result = coll.insert(doc); return (ObjectId) result.getUpsertedId(); }
From source file:org.apache.chemistry.opencmis.mongodb.MongodbUtils.java
License:Apache License
public BasicDBObject addNode(BasicDBObject node, BasicDBObject parent) { // Update all affected right and left values which are greater or equal // to the parents right value - we are incrementing to 'make room' for the // new node//from w ww. j a v a2 s. c o m db.getCollection(COLLECTION_CONTENT).update( new BasicDBObject().append("right", new BasicDBObject().append("$gte", parent.getLong("right"))), new BasicDBObject().append("$inc", new BasicDBObject().append("right", 2)), false, true); db.getCollection(COLLECTION_CONTENT).update( new BasicDBObject().append("left", new BasicDBObject().append("$gte", parent.getLong("right"))), new BasicDBObject().append("$inc", new BasicDBObject().append("left", 2)), false, true); // Finally insert the node into the created space in the tree, under the parent node.append("left", parent.getLong("right")).append("right", parent.getLong("right") + 1).append("level", parent.getLong("level") + 1); WriteResult result = db.getCollection(COLLECTION_CONTENT).insert(node); if (result.getN() != 1) { throw new MongoException("Error while inserting the node into the database."); } else { return node.append("_id", result.getUpsertedId()); } }
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);//from w ww.java 2s . 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)); }