List of usage examples for com.mongodb WriteResult toString
@Override
public String toString()
From source file:mongodb.JavaDocUpdate.java
public static void resetDoc(DBCollection collection) { BasicDBObject query = new BasicDBObject("word", "lefty"); BasicDBObject update = new BasicDBObject(); update.append("$set", new BasicDBObject("word", "left")); BasicDBObject inc = new BasicDBObject("size", -1); inc.append("stats.consonants", -1); update.append("$inc", inc); update.append("$pop", new BasicDBObject("letters", 1)); WriteResult result = collection.update(query, update, false, false); System.out.println("Reset Result: \n" + result.toString()); }
From source file:mongodb.JavaDocUpsert.java
public static void addUpsert(DBCollection collection) { BasicDBObject query = new BasicDBObject("word", "righty"); BasicDBObject setOp = new BasicDBObject("word", "righty"); setOp.append("first", "l").append("last", "y"); setOp.append("size", 4).append("category", "New"); BasicDBObject stats = new BasicDBObject("consonants", 4); stats.append("vowels", 1); setOp.append("stats", stats); setOp.append("letters", new String[] { "r", "i", "g", "h" }); BasicDBObject cons = new BasicDBObject("type", "consonants"); cons.append("chars", new String[] { "r", "g", "h" }); BasicDBObject vowels = new BasicDBObject("type", "vowels"); vowels.append("chars", new String[] { "i" }); BasicDBObject[] charsets = new BasicDBObject[] { cons, vowels }; setOp.append("charsets", charsets); BasicDBObject update = new BasicDBObject("$set", setOp); WriteResult result = collection.update(query, update, true, false); System.out.println("Update as insert Result: \n" + result.toString()); JavaDocUpsert.showWord(collection, "After upsert as insert"); }
From source file:mongodb.JavaDocUpsert.java
public static void updateUpsert(DBCollection collection) { BasicDBObject query = new BasicDBObject("word", "righty"); BasicDBObject setOp = new BasicDBObject("word", "righty"); setOp.append("first", "l").append("last", "y"); setOp.append("size", 6).append("category", "New"); BasicDBObject stats = new BasicDBObject("consonants", 5); stats.append("vowels", 1); setOp.append("stats", stats); setOp.append("letters", new String[] { "r", "i", "g", "h", "t", "y" }); BasicDBObject cons = new BasicDBObject("type", "consonants"); cons.append("chars", new String[] { "r", "g", "h", "t", "y" }); BasicDBObject vowels = new BasicDBObject("type", "vowels"); vowels.append("chars", new String[] { "i" }); BasicDBObject[] charsets = new BasicDBObject[] { cons, vowels }; setOp.append("charsets", charsets); BasicDBObject update = new BasicDBObject("$set", setOp); WriteResult result = collection.update(query, update, true, false); System.out.println("Update as insert Result: \n" + result.toString()); JavaDocUpsert.showWord(collection, "After upsert as update"); JavaDocUpsert.cleanupWord(collection); }
From source file:net.kamradtfamily.mongorest.AdminServlet.java
License:GNU General Public License
@Override protected void doDelete(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { log.fine("doDelete()"); String db_name = req.getParameter("dbname"); String user = req.getParameter("user"); if (db_name == null || user == null) { error(res, SC_BAD_REQUEST, Status.get("param name missing")); return;/* w w w . ja v a2 s.c o m*/ } DB db = mongo.getDB(db_name); WriteResult o = db.removeUser(user); out_str(req, o.toString(), "application/json"); }
From source file:net.kamradtfamily.mongorest.AdminServlet.java
License:GNU General Public License
@Override protected void doPut(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { log.fine("doPut()"); String db_name = req.getParameter("dbname"); if (db_name == null) { error(res, SC_BAD_REQUEST, Status.get("param name missing")); return;//from w w w .j a v a 2 s .c o m } boolean read_only = Boolean.parseBoolean(req.getParameter("readonly")); DB db = mongo.getDB(db_name); WriteResult o = db.addUser("me", "me".toCharArray()); out_str(req, o.toString(), "application/json"); }
From source file:org.aw20.mongoworkbench.command.RemoveMongoCommand.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);// ww w.ja va 2 s . c o m BasicDBObject cmdMap = parseMongoCommandString(db, cmd); if (!cmdMap.containsField("removeArgs")) throw new Exception("no remove document"); DBCollection collection = db.getCollection(sColl); BasicDBList args = (BasicDBList) cmdMap.get("removeArgs"); // Run the command db.requestStart(); WriteResult writeresult; try { writeresult = collection.remove((DBObject) args.get(0), WriteConcern.JOURNAL_SAFE); } 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("Removed"); }
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);// ww w .j a va 2s . 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.UpdateMongoCommand.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 ww w .j a v a2s . c o m BasicDBObject cmdMap = parseMongoCommandString(db, cmd); if (!cmdMap.containsField("updateArg")) throw new Exception("no update document"); List argList = (List) cmdMap.get("updateArg"); if (argList.size() == 1) throw new Exception("not enough parameters; db.collection.update(query, update, <upsert>, <multi>)"); DBCollection collection = db.getCollection(sColl); db.requestStart(); WriteResult writeresult = null; try { if (argList.size() == 2) { writeresult = collection.update((DBObject) argList.get(0), fixNumbers((BasicDBObject) argList.get(1))); } else if (argList.size() == 3) { boolean upsert = StringUtil.toBoolean(argList.get(2), false); writeresult = collection.update((DBObject) argList.get(0), fixNumbers((BasicDBObject) argList.get(1)), upsert, false); } else if (argList.size() == 4) { boolean upsert = StringUtil.toBoolean(argList.get(2), false); boolean multi = StringUtil.toBoolean(argList.get(3), false); writeresult = collection.update((DBObject) argList.get(0), fixNumbers((BasicDBObject) argList.get(1)), upsert, multi); } else throw new Exception("too many parameters; db.collection.update(query, update, <upsert>, <multi>)"); } 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("Updated: updatedExisting=" + mwriteresult.get("updatedExisting") + "; documentsUpdated=" + mwriteresult.get("n")); }
From source file:org.basex.modules.MongoDB.java
License:BSD License
/** * Insert data in MongoDB./* w w w . j a v a 2 s. c o m*/ * @param handler DB Handler * @param col Collection name * @param insertString string to insert in json formart or in Basex's Map * @return Item result from Mongodb. * @throws Exception */ public Item insert(final Str handler, final Str col, final Str insertString) throws Exception { final DB db = getDbHandler(handler); db.requestStart(); try { DBObject obj = getDbObjectFromStr(insertString); WriteResult wr = db.getCollection(col.toJava()).insert(obj); return returnResult(handler, Str.get(wr.toString())); } catch (MongoException e) { throw MongoDBErrors.generalExceptionError(db.getLastError().getString("err")); } finally { db.requestDone(); } }
From source file:org.basex.modules.MongoDB.java
License:BSD License
/** * Mongodb update with 4 parameters like update({},{}, upsert, multi). * @param handler Db Handler string/* ww w . j a v a 2 s . c o m*/ * @param col Collection name * @param updatestring String to be updated * @param upsert true/false for mongodb upsert(Json Str or Map) * @param multi true/false for mongodb multi * @return Item * @throws Exception */ public Item update(final Str handler, final Item col, final Item query, final Str updatestring, final Bln upsert, final Bln multi) throws Exception { final DB db = getDbHandler(handler); db.requestStart(); try { DBObject q = getDbObjectFromStr(query); DBObject updateValue = getDbObjectFromStr(updatestring); WriteResult wr; if (upsert != null && multi != null) { wr = db.getCollection(itemToString(col)).update(q, updateValue, upsert.toJava(), multi.toJava()); } else { wr = db.getCollection(itemToString(col)).update(q, updateValue); } return returnResult(handler, Str.get(wr.toString())); } catch (MongoException e) { throw MongoDBErrors.generalExceptionError(db.getLastError().getString("err")); } finally { db.requestDone(); } }