List of usage examples for com.mongodb DBCollection remove
public WriteResult remove(final DBObject query)
From source file:com.sample.MyGroceryListServlet.java
License:Open Source License
/** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) *//*from w ww . j av a 2 s.com*/ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Reference: http://docs.mongodb.org/ecosystem/tutorial/getting-started-with-java-driver/#getting-started-with-java-driver String envVars = System.getenv("VCAP_SERVICES"); DBObject dbO = (DBObject) JSON.parse(envVars); String parsedString = dbO.get("mongodb").toString(); // Remove trailing and starting array brackets (otherwise it won't be valid JSON) parsedString = parsedString.replaceFirst("\\[ ", ""); parsedString = parsedString.replaceFirst("\\]$", ""); // Get the credentials dbO = (DBObject) JSON.parse(parsedString); parsedString = dbO.get("credentials").toString(); // For debugging only // System.out.println(parsedString); dbO = (DBObject) JSON.parse(parsedString); System.out.println("Host name : " + dbO.get("hostname")); String hostName = dbO.get("hostname").toString(); int port = Integer.parseInt(dbO.get("port").toString()); String dbName = dbO.get("db").toString(); String userName = dbO.get("username").toString(); String password = dbO.get("password").toString(); Mongo mongoClient = new Mongo(hostName, port); DB db = mongoClient.getDB(dbName); db.authenticate(userName, password.toCharArray()); // Clean up old entries DBCollection coll = db.getCollection("testCollection"); coll.drop(); BasicDBObject lastAddedObject = null; for (String curItem : myGroceryList) { lastAddedObject = new BasicDBObject("i", curItem); coll.insert(lastAddedObject); } response.getWriter().println("<b>My grocery list is:</b>"); coll.remove(lastAddedObject); DBCollection loadedCollection = db.getCollection("testCollection"); DBCursor cursor = loadedCollection.find(); try { response.getWriter().println("<ul>"); while (cursor.hasNext()) { response.getWriter().println("<li>" + cursor.next().get("i") + "</li>"); } response.getWriter().println("</ul>"); } finally { cursor.close(); } }
From source file:com.sanaldiyar.projects.nanohttpd.mongodbbasedsessionhandler.MongoDBBasedSessionHandler.java
/** * Request parser for session. Gets and builds session information * * @param request the request// w w w. j ava 2s .c o m * @return session manager */ @Override public NanoSessionManager parseRequest(Request request) { MongoDBBasedSessionManager nanoSessionManager = null; DBObject session = null; String sessionid = null; for (Cookie cookie : request.getCookies()) { if (cookie.getName().equals(SESSIONCOOKIEID)) { sessionid = cookie.getValue(); break; } } DBCollection sessions = managers.getCollection("sessions"); if (sessionid != null) { DBCursor cursor = sessions.find(new BasicDBObject("sessionid", sessionid)); List<DBObject> result = cursor.toArray(); cursor.close(); if (result.size() == 1) { session = result.get(0); } if (session != null) { if (((Date) session.get("expires")).getTime() <= new Date().getTime()) { sessions.remove(new BasicDBObject().append("sessionid", sessionid)); session = null; } } } if (session == null) { do { sessionid = new BigInteger(128, srng).toString(32); } while (sessions.findOne(new BasicDBObject().append("sessionid", sessionid)) != null && !sessionid.equals("0")); session = new BasicDBObject(); nanoSessionManager = new MongoDBBasedSessionManager(session); nanoSessionManager.setSessionID(sessionid); sessions.insert(session); } else { nanoSessionManager = new MongoDBBasedSessionManager(session); } return nanoSessionManager; }
From source file:com.sanaldiyar.projects.nanohttpd.mongodbbasedsessionhandler.MongoDBBasedSessionHandler.java
/** * Session manager cleaner method/*from w w w. j a v a 2 s.c om*/ */ public void cleanSesssionManagers() { DBCollection sessions = managers.getCollection("sessions"); sessions.remove(new BasicDBObject("expires", new BasicDBObject("$lte", new Date()))); }
From source file:com.sitewhere.mongodb.MongoPersistence.java
License:Open Source License
/** * Common handler for deleting objects. Assures that errors are handled in a * consistent way./*from w ww . j av a 2 s .c om*/ * * @param collection * @param object * @return * @throws SiteWhereException */ public static WriteResult delete(DBCollection collection, DBObject object) throws SiteWhereException { WriteResult result = collection.remove(object); if (!result.getLastError().ok()) { throw new SiteWhereException("Error during delete: " + result.getLastError().toString()); } return result; }
From source file:com.softlyinspired.jlw.script.validationScript.java
License:Open Source License
/** * delete the script from the mongodb/*from w w w.java 2s.c o m*/ * @param ScriptId * @throws UnknownHostException */ public void deleteScript(int ScriptId) throws UnknownHostException { DBCollection coll = repoConnection.getScriptCollection(); try { BasicDBObject query = new BasicDBObject(); query.put("scriptId", ScriptId); coll.remove(query); } catch (Exception e) { JLWUtilities.scriptErrorMessage("Error deleting script"); } }
From source file:com.spring.tutorial.controllers.DefaultController.java
@RequestMapping(value = "/my-drive/delete", method = RequestMethod.POST) public @ResponseBody String delete(HttpServletRequest request) throws UnknownHostException, Exception { MongoData mongoData = new MongoData(); DB db = mongoData.getDB();/* w w w .ja v a2 s . c o m*/ DBCollection collection = db.getCollection(request.getSession().getAttribute("id") + "_files_meta"); BasicDBObject document = new BasicDBObject(); String fileID = request.getParameter("fileID").toString(); document.put("file-id", new ObjectId(fileID)); collection.remove(document); document = new BasicDBObject(); GridFS gfs = new GridFS(db, request.getSession().getAttribute("id").toString() + "_files.files"); document.put("_id", new ObjectId(fileID)); gfs.remove(document); collection.remove(document); return fileID; }
From source file:com.spring.tutorial.controllers.DefaultController.java
@RequestMapping(value = "/my-drive/dropbox/delete", method = RequestMethod.POST) public @ResponseBody String deleteDropboxFile(HttpServletRequest request) throws DbxException, UnknownHostException, Exception { String path = request.getParameter("path"); DbxRequestConfig config = new DbxRequestConfig("JavaTutorial/1.0", Locale.getDefault().toString()); DbxClient client = new DbxClient(config, (String) request.getSession().getAttribute("dropbox_token")); client.delete(path);/*from w w w. j a v a2 s . c o m*/ MongoData mongoData = new MongoData(); DB db = mongoData.getDB(); DBCollection collection = db.getCollection(request.getSession().getAttribute("id") + "_dropbox_files_meta"); BasicDBObject document = new BasicDBObject(); document.put("path", path); collection.remove(document); return "sucessfull"; }
From source file:com.spring.tutorial.dropbox.DropBoxAuth.java
public void removeJunk(String id, DbxClient client, String path) throws UnknownHostException, Exception { List<String> paths = new ArrayList<>(); getDropboxPaths(id, paths, "/"); MongoData mongoData = new MongoData(); DB db = mongoData.getDB();/*from w ww . ja v a2s.c om*/ DBCollection collection = db.getCollection(id + "_dropbox_files_meta"); DBCursor cursor = collection.find(); if (cursor.count() != 0) { while (cursor.hasNext()) { boolean found = false; DBObject doc = cursor.next(); for (String onePath : paths) { if (doc.get("path").equals(onePath)) { found = true; } } if (found == false) { collection.remove(doc); } } } }
From source file:com.stratio.connector.mongodb.core.engine.MongoStorageEngine.java
License:Apache License
@Override protected void delete(TableName tableName, Collection<Filter> whereClauses, Connection<MongoClient> connection) throws ExecutionException, UnsupportedException { DB db = connection.getNativeConnection().getDB(tableName.getCatalogName().getName()); if (db.collectionExists(tableName.getName())) { DBCollection coll = db.getCollection(tableName.getName()); try {// w ww .ja v a2 s .c o m coll.remove(buildFilter(whereClauses)); } catch (MongoException e) { logger.error("Error deleting the data: " + e.getMessage()); throw new MongoDeleteException(e.getMessage(), e); } } }
From source file:com.stratio.qa.utils.MongoDBUtils.java
License:Apache License
/** * Drop all the data associated to a MongoDB Collection. * * @param collectionName/* www. j a va2s .co m*/ */ public void dropAllDataMongoDBCollection(String collectionName) { DBCollection db = getMongoDBCollection(collectionName); DBCursor objectsList = db.find(); try { while (objectsList.hasNext()) { db.remove(objectsList.next()); } } finally { objectsList.close(); } }