List of usage examples for com.mongodb MongoClient getDB
@Deprecated public DB getDB(final String dbName)
From source file:org.aw20.mongoworkbench.command.GridFSPutFileCommand.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); GridFS gfs = new GridFS(db, sColl.substring(0, sColl.lastIndexOf("."))); GridFSInputFile gridFSInputFile = gfs.createFile(getFile); gridFSInputFile.setContentType(MimetypesFileTypeMap.getDefaultFileTypeMap().getContentType(getFile)); gridFSInputFile.save();//w w w. j a v a 2 s .co m setMessage("fileLoaded=" + getFile + "; size=" + getFile.length()); }
From source file:org.aw20.mongoworkbench.command.GridFSRemoveBucketCommand.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); db.getCollection(sColl + ".files").drop(); db.getCollection(sColl + ".chunks").drop(); setMessage("bucketDrop=" + sColl); }
From source file:org.aw20.mongoworkbench.command.GridFSRemoveFileCommand.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); GridFS gfs = new GridFS(db, sColl.substring(0, sColl.lastIndexOf("."))); gfs.remove(new ObjectId(id)); setMessage("fileRemoved=" + id); }
From source file:org.aw20.mongoworkbench.command.GroupMongoCommand.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); BasicDBObject cmdMap = parseMongoCommandString(db, cmd); if (!cmdMap.containsField("groupArg")) throw new Exception("no group document"); // Execute the command Object result = db.eval(cmd, (Object[]) null); if (result == null) throw new Exception("null returned"); if (!(result instanceof BasicDBList)) throw new Exception("not correct type returned: " + result.getClass().getName()); dbListResult = (BasicDBList) result; setMessage("# rows=" + dbListResult.size()); }
From source file:org.aw20.mongoworkbench.command.MapReduceMongoCommand.java
License:Open Source License
@SuppressWarnings("deprecation") @Override/* w w w. ja v a 2 s .c o m*/ 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); BasicDBObject cmdMap = parseMongoCommandString(db, cmd); if (!cmdMap.containsField("mapreduceArgs")) throw new Exception("no mapReduce document"); DBCollection collection = db.getCollection(sColl); // Build the Map BasicDBObject options = (BasicDBObject) ((BasicDBList) cmdMap.get("mapreduceArgs")).get(2); String outputCollection = null; String outputDB = null; MapReduceCommand.OutputType outputType = MapReduceCommand.OutputType.INLINE; if (options.get("out") instanceof String) { outputCollection = (String) options.get("out"); outputType = MapReduceCommand.OutputType.REPLACE; } else if (options.get("out") instanceof BasicDBObject) { BasicDBObject out = (BasicDBObject) options.get("out"); if (out.containsField("inline")) { outputCollection = null; } else if (out.containsField("replace")) { outputCollection = (String) out.get("replace"); outputType = MapReduceCommand.OutputType.REPLACE; } else if (out.containsField("merge")) { outputCollection = (String) out.get("merge"); outputType = MapReduceCommand.OutputType.MERGE; } else if (out.containsField("reduce")) { outputCollection = (String) out.get("reduce"); outputType = MapReduceCommand.OutputType.REDUCE; } if (out.containsField("db")) outputDB = (String) out.get("db"); } MapReduceCommand mrc = new MapReduceCommand(collection, ((Code) ((BasicDBList) cmdMap.get("mapreduceArgs")).get(0)).getCode(), ((Code) ((BasicDBList) cmdMap.get("mapreduceArgs")).get(1)).getCode(), outputCollection, outputType, (BasicDBObject) options.get("query")); if (outputDB != null) mrc.setOutputDB(outputDB); if (options.containsField("sort") && options.get("sort") instanceof DBObject) mrc.setSort((DBObject) options.get("sort")); if (options.containsField("scope") && options.get("scope") instanceof DBObject) mrc.setScope(((DBObject) options.get("scope")).toMap()); if (options.containsField("finalize") && options.get("scope") instanceof Code) mrc.setFinalize(((Code) options.get("scope")).getCode()); if (options.containsField("limit")) mrc.setLimit(StringUtil.toInteger(options.get("limit"), -1)); mrc.addExtraOption("jsMode", StringUtil.toBoolean(options.get("jsMode"), false)); mrc.setVerbose(StringUtil.toBoolean(options.get("verbose"), false)); // Run the actual mapreduce function MapReduceOutput mro = collection.mapReduce(mrc); // Pull the inline results if (mro.getOutputCollection() == null) { dbListResult = new BasicDBList(); Iterable<DBObject> it = mro.results(); for (DBObject dbo : it) { dbListResult.add(dbo); } } BasicDBObject dbo = mro.getRaw(); StringBuilder sb = new StringBuilder(); if (dbo.containsField("timeMillis")) sb.append("Time=").append(dbo.get("timeMillis")).append("ms; "); if (dbo.containsField("counts")) { BasicDBObject counts = (BasicDBObject) dbo.get("counts"); sb.append("Counts: input=" + counts.get("input")); sb.append("; emit=" + counts.get("emit")); sb.append("; reduce=" + counts.get("reduce")); sb.append("; output=" + counts.get("output")); } setMessage(sb.toString()); }
From source file:org.aw20.mongoworkbench.command.MongoCommand.java
License:Open Source License
public BasicDBObject parse() 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); return parseMongoCommandString(db, cmd); }
From source file:org.aw20.mongoworkbench.command.PassThruMongoCommand.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); resultObj = db.eval(cmd, (Object[]) null); }
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); 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();// w w w.j ava2 s . c o m 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); 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();/*from ww w .j a v a 2s. c o m*/ 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")); }