List of usage examples for com.mongodb DBCollection save
public WriteResult save(final DBObject document, final WriteConcern writeConcern)
From source file:com.socialsky.mods.MongoPersistor.java
License:Apache License
private void doSave(Message<JsonObject> message) { String collection = getMandatoryString("collection", message); if (collection == null) { return;//from www . j av a 2 s.c o m } JsonObject doc = getMandatoryObject("document", message); if (doc == null) { return; } String genID; if (doc.getField("_id") == null) { genID = UUID.randomUUID().toString(); doc.putString("_id", genID); } else { genID = null; } DBCollection coll = db.getCollection(collection); DBObject obj = jsonToDBObject(doc); WriteConcern writeConcern = WriteConcern.valueOf(getOptionalStringConfig("writeConcern", "")); // Backwards compatibility if (writeConcern == null) { writeConcern = WriteConcern.valueOf(getOptionalStringConfig("write_concern", "")); } if (writeConcern == null) { writeConcern = db.getWriteConcern(); } WriteResult res = coll.save(obj, writeConcern); if (res.getError() == null) { if (genID != null) { JsonObject reply = new JsonObject(); reply.putString("_id", genID); sendOK(message, reply); } else { sendOK(message); } } else { sendError(message, res.getError()); } }
From source file:com.streamreduce.core.dao.MetricDAO.java
License:Apache License
@Override public Key<Metric> save(Metric entity, WriteConcern wc) { DBCollection collection = getCollection(entity.getAccountId()); DBObject dbObject = entity.toDBObject(); collection.save(dbObject, wc); entity.setId((ObjectId) dbObject.get("_id")); return new Key<>(Metric.class, entity.getId()); }
From source file:com.tomtom.speedtools.mongodb.DaoUtils.java
License:Apache License
/** * Store the given object in the given collections. * * @param <T> The type of the object to te stored. * @param collection Collection in which to store the object. * @param mapper Mapper to be used to transform the object. * @param entity The object to be stored. * @throws EntityStoreException Thrown if the object cannot be transformed. The error will have been logged. *//*from w w w.j a v a 2 s . co m*/ public static <T> void storeEntity(@Nonnull final DBCollection collection, @Nonnull final EntityMapper<T> mapper, @Nonnull final T entity) throws EntityStoreException { assert collection != null; assert mapper != null; assert entity != null; try { // Create MongoDB record. final DBObject dbObject = mapper.toDb(entity); // Cannot be null because entity was not null. assert dbObject != null; // Add a "last modified" timestamp to each record. dbObject.put(MongoDBKeyNames.LAST_MODIFIED_KEY, UTCTime.now().toDate()); collection.save(dbObject, writeConcern); } catch (final MapperException | MongoException e) { final String message = "Map entity failed: type=" + entity.getClass().getSimpleName() + ", collection=" + collection.getName() + '.'; LOG.error("storeEntity: " + message, e); throw new EntityStoreException(message, e); } }
From source file:me.konglong.momei.mongodb.core.MongoScriptExecutor.java
License:Apache License
private Object saveDBObject(final String collectionName, final DBObject dbDoc, final Optional<WriteConcern> writeConcern) { return execute(collectionName, new CollectionCallback<Object>() { @Override//from www. ja v a 2 s. c o m public Object doInCollection(DBCollection collection) throws MongoException { if (writeConcern.isPresent()) { collection.save(dbDoc, writeConcern.get()); } else { collection.save(dbDoc); } return dbDoc.get(ID_FIELD); } }); }
From source file:org.aw20.mongoworkbench.command.SaveMongoCommand.java
License:Open Source License
@Override public void execute() throws Exception { MongoClient mdb = MongoFactory.getInst().getMongo(sName); if (mdb == null) throw new Exception("no server selected"); if (sDb == null) throw new Exception("no database selected"); MongoFactory.getInst().setActiveDB(sDb); DB db = mdb.getDB(sDb);//from w w w . j a v a 2 s . c o m BasicDBObject cmdMap = parseMongoCommandString(db, cmd); if (!cmdMap.containsField("saveArg")) throw new Exception("no save document"); DBObject document = fixNumbers((BasicDBObject) cmdMap.get("saveArg")); DBCollection collection = db.getCollection(sColl); // Run the command db.requestStart(); WriteResult writeresult; try { writeresult = collection.save(document, WriteConcern.JOURNAL_SAFE); id = document.get("_id"); } finally { db.requestDone(); } // Get the result Map mwriteresult = (Map) JSON.parse(writeresult.toString()); mwriteresult.put("exeDate", new Date()); EventWorkBenchManager.getInst().onEvent(Event.WRITERESULT, mwriteresult); setMessage("Saved: updatedExisting=" + mwriteresult.get("updatedExisting") + "; documentsUpdated=" + mwriteresult.get("n")); }
From source file:org.aw20.mongoworkbench.command.SystemJavaScriptWriteCommand.java
License:Open Source License
@Override public void execute() throws Exception { MongoClient mdb = MongoFactory.getInst().getMongo(sName); if (mdb == null) throw new Exception("no server selected"); if (sDb == null) throw new Exception("no database selected"); MongoFactory.getInst().setActiveDB(sDb); DB db = mdb.getDB(sDb);//w ww . j av a2 s . c om DBCollection coll = db.getCollection("system.js"); BasicDBObject dbo = new BasicDBObject("_id", jsName).append("value", new Code(jsCode)); coll.save(dbo, WriteConcern.JOURNAL_SAFE); setMessage("System JavaScript Written=" + jsName); }
From source file:org.benjp.services.mongodb.ChatServiceImpl.java
License:Open Source License
public void delete(String room, String user, String messageId) { DBCollection coll = db().getCollection(M_ROOM_PREFIX + room); BasicDBObject query = new BasicDBObject(); query.put("_id", new ObjectId(messageId)); query.put("user", user); DBCursor cursor = coll.find(query);/*from w ww. ja v a 2s . c om*/ if (cursor.hasNext()) { DBObject dbo = cursor.next(); dbo.put("message", TYPE_DELETED); dbo.put("type", TYPE_DELETED); coll.save(dbo, WriteConcern.NONE); } }
From source file:org.benjp.services.mongodb.ChatServiceImpl.java
License:Open Source License
public void edit(String room, String user, String messageId, String message) { DBCollection coll = db().getCollection(M_ROOM_PREFIX + room); message = StringUtils.chomp(message); message = message.replaceAll("&", "&"); message = message.replaceAll("<", "<"); message = message.replaceAll(">", ">"); message = message.replaceAll("\"", """); message = message.replaceAll("\n", "<br/>"); message = message.replaceAll("\\\\", "\"); BasicDBObject query = new BasicDBObject(); query.put("_id", new ObjectId(messageId)); query.put("user", user); DBCursor cursor = coll.find(query);/*from w w w.ja v a 2 s .c o m*/ if (cursor.hasNext()) { DBObject dbo = cursor.next(); dbo.put("message", message); dbo.put("type", TYPE_EDITED); coll.save(dbo, WriteConcern.NONE); } }
From source file:org.benjp.services.mongodb.ChatServiceImpl.java
License:Open Source License
private void updateRoomTimestamp(String room) { DBCollection coll = db().getCollection(M_ROOM_PREFIX + M_ROOMS_COLLECTION); BasicDBObject basicDBObject = new BasicDBObject(); basicDBObject.put("_id", room); DBCursor cursor = coll.find(basicDBObject); if (cursor.hasNext()) { DBObject dbo = cursor.next();//from www. j a v a 2 s . co m dbo.put("timestamp", System.currentTimeMillis()); coll.save(dbo, WriteConcern.NONE); } }
From source file:org.benjp.services.mongodb.ChatServiceImpl.java
License:Open Source License
public void setRoomName(String room, String name) { DBCollection coll = db().getCollection(M_ROOM_PREFIX + M_ROOMS_COLLECTION); BasicDBObject basicDBObject = new BasicDBObject(); basicDBObject.put("_id", room); DBCursor cursor = coll.find(basicDBObject); if (cursor.hasNext()) { DBObject dbo = cursor.next();/*from w w w. j a v a 2 s . c o m*/ dbo.put("team", name); coll.save(dbo, WriteConcern.NONE); } }