List of usage examples for com.mongodb DBCollection update
public WriteResult update(final DBObject query, final DBObject update)
From source file:com.softlyinspired.jlw.script.validationScript.java
License:Open Source License
/** * Write details of existing script to mongodb * @param scriptId/* w ww. j a v a2 s.co m*/ * @param scriptText * @param scriptCategory * @param scriptTitle * @param scriptConnection * @throws UnknownHostException */ private void writeExistingScript(int scriptId, String scriptText, String scriptCategory, String scriptTitle, String scriptConnection) throws UnknownHostException { DBCollection coll = repoConnection.getScriptCollection(); BasicDBObject updateQuery = new BasicDBObject(); BasicDBObject searchQuery = new BasicDBObject().append("scriptId", scriptId); DBObject changes = new BasicDBObject().append("scriptText", scriptText) .append("ScriptCategory", scriptCategory).append("scriptTitle", scriptTitle) .append("scriptConnection", scriptConnection); updateQuery.append("$set", changes); try { coll.update(searchQuery, updateQuery); } catch (Exception e) { JLWUtilities.scriptErrorMessage("Error creating script"); } }
From source file:com.spring.tutorial.controllers.DefaultController.java
@RequestMapping(value = "/dropbox-auth-finished", method = RequestMethod.GET) public String authenticated(HttpServletResponse response, HttpServletRequest request, ModelMap map) throws DbxException, IOException { DbxAuthFinish authFinish;//w w w.j av a 2 s .c om String returnTo = "mydrive/mydrive"; final String APP_KEY = "mt6yctmupgrodlm"; final String APP_SECRET = "p68kiiyvdctftuv"; DbxAppInfo appInfo = new DbxAppInfo(APP_KEY, APP_SECRET); try { String redirectUrl = getUrl(request, "http://localhost:8080/FOU_1/dropbox-auth-finished"); // Select a spot in the session for DbxWebAuth to store the CSRF token. HttpSession session = request.getSession(); String sessionKey = "dropbox-auth-csrf-token"; DbxSessionStore csrfTokenStore = new DbxStandardSessionStore(session, sessionKey); auth = new DbxWebAuth(getRequestConfig(request), appInfo, redirectUrl, csrfTokenStore); authFinish = auth.finish(request.getParameterMap()); } catch (DbxWebAuth.BadRequestException ex) { log("On /dropbox-auth-finish: Bad request: " + ex.getMessage()); response.sendError(400); return returnTo; } catch (DbxWebAuth.BadStateException ex) { // Send them back to the start of the auth flow. response.sendRedirect("http://localhost:8080/FOU_1/dropbox-auth-finished" + ex.getMessage()); return returnTo; } catch (DbxWebAuth.CsrfException ex) { log("On /dropbox-auth-finish: CSRF mismatch: " + ex.getMessage()); return returnTo; } catch (DbxWebAuth.NotApprovedException ex) { return "redirect:my-drive/dropbox/home"; } catch (DbxWebAuth.ProviderException ex) { log("On /dropbox-auth-finish: Auth failed: " + ex.getMessage()); response.sendError(503, "Error communicating with Dropbox." + ex.getMessage()); return returnTo; } catch (DbxException ex) { log("On /dropbox-auth-finish: Error getting token: " + ex.getMessage()); response.sendError(503, "Error communicating with Dropbox." + ex.getMessage()); return returnTo; } String accessToken = authFinish.accessToken; DbxClient client = new DbxClient(config, accessToken); MongoClient mongoClient = new MongoClient(); DB db = mongoClient.getDB("fou"); char[] pass = "mongo".toCharArray(); boolean auth = db.authenticate("emime", pass); DBCollection collection = db.getCollection("users"); if (auth == true) { BasicDBObject newDocument = new BasicDBObject(); newDocument.append("$set", new BasicDBObject().append("dropbox_token", accessToken)); BasicDBObject searchQuery = new BasicDBObject().append("id", request.getSession().getAttribute("id")); collection.update(searchQuery, newDocument); } map.addAttribute("title", "my-drive"); request.getSession().setAttribute("dropbox_token", accessToken); response.sendRedirect("http://localhost:8080/FOU_1/my-drive/dropbox/home"); return "mydrive/dropbox"; }
From source file:com.spring.tutorial.controllers.DefaultController.java
@RequestMapping(value = "/my-drive/dropbox/edit", method = RequestMethod.POST) public @ResponseBody String editDropboxFile(HttpServletRequest request) throws IOException, DbxException, Exception { String tags = request.getParameter("tags"); String description = request.getParameter("description"); String path = request.getParameter("path"); MongoData mongoData = new MongoData(); DB db = mongoData.getDB();/* w w w. j av a 2 s .c o m*/ DBCollection collection = db.getCollection(request.getSession().getAttribute("id") + "_dropbox_files_meta"); BasicDBObject document = new BasicDBObject(); document.append("tags", tags); document.append("description", description); BasicDBObject newDocument = new BasicDBObject(); newDocument.append("$set", document); BasicDBObject searchQuery = new BasicDBObject().append("path", path); collection.update(searchQuery, newDocument); return "success"; }
From source file:com.spring.tutorial.dropbox.DropBoxAuth.java
public void getDropboxPaths(String id, List<String> paths, String path) throws DbxException, UnknownHostException, Exception { DbxEntry.WithChildren listing = client.getMetadataWithChildren(path); for (DbxEntry child : listing.children) { if (child.isFolder()) { getDropboxPaths(id, paths, child.path); }// w w w . j a va2 s . c o m paths.add(child.path); } MongoData mongoData = new MongoData(); DB db = mongoData.getDB(); DBCollection collection = db.getCollection("users"); DBObject document = new BasicDBObject(); document.put("dropbox_hash", listing.hash); BasicDBObject newDocument = new BasicDBObject(); newDocument.append("$set", document); BasicDBObject searchQuery = new BasicDBObject().append("id", id); collection.update(searchQuery, newDocument); }
From source file:com.spring.tutorial.dropbox.DropBoxAuth.java
public void fetchFilesAndFolders(String id, DbxClient client, String path) throws DbxException, UnknownHostException, Exception { MongoData mongoData = new MongoData(); DB db = mongoData.getDB();/*from w w w . ja v a 2 s . c o m*/ DBCollection collection = db.getCollection(id + "_dropbox_files_meta"); DBCursor cursor = collection.find(); DbxEntry.WithChildren listing = client.getMetadataWithChildren(path); for (DbxEntry child : listing.children) { String fileInfo = child.toString(); DropboxEntity entity; if (child.isFile()) { String rev = fileInfo.substring(fileInfo.indexOf("rev=") + 5, fileInfo.indexOf(")") - 1); String fileSize = fileInfo.substring(fileInfo.indexOf("humanSize=") + 11, fileInfo.indexOf("\", lastModified")); String fileLastModified = fileInfo.substring(fileInfo.indexOf("lastModified=") + 14, fileInfo.indexOf("\", clientMtime")); entity = new DropboxEntity(child.name, "file", fileSize, fileLastModified, child.path, rev); DBObject document = new BasicDBObject(); document.put("rev", rev); document.put("fileSize", fileSize); document.put("lastModified", fileLastModified); document.put("name", child.name); document.put("path", child.path); document.put("type", "file"); cursor = collection.find(); boolean found = false; while (cursor.hasNext()) { DBObject doc = cursor.next(); String path1 = (String) doc.get("path"); if (path1.equals(child.path)) { found = true; } } if (found) { document.put("found", "true"); BasicDBObject newDocument = new BasicDBObject(); newDocument.append("$set", document); BasicDBObject searchQuery = new BasicDBObject().append("path", child.path); collection.update(searchQuery, newDocument); } else { document.put("tags", ""); document.put("description", ""); document.put("found", "false"); collection.insert(document); } } else { entity = new DropboxEntity(child.name, "folder", child.path); DBObject document = new BasicDBObject(); document.put("rev", ""); document.put("fileSize", ""); document.put("lastModified", ""); document.put("name", child.name); document.put("path", child.path); document.put("type", "folder"); cursor = collection.find(); boolean found = false; if (cursor.hasNext()) { while (cursor.hasNext()) { DBObject doc = cursor.next(); String path1 = (String) doc.get("path"); if (path1.equals(child.path)) { found = true; } } } if (found) { document.put("found", "true"); BasicDBObject newDocument = new BasicDBObject(); newDocument.append("$set", document); BasicDBObject searchQuery = new BasicDBObject().append("path", child.path); collection.update(searchQuery, newDocument); } else { document.put("tags", ""); document.put("description", ""); document.put("found", "false"); collection.insert(document); } fetchFilesAndFolders(id, client, child.path); } } }
From source file:com.staticvillage.recommender.indexer.MongoDBIndexer.java
License:Apache License
@Override public boolean updateBean(Place bean) { DBCollection dbCollection = instanceDB.getCollection(collection); BasicDBObject item = new BasicDBObject("id", bean.id); WriteResult writeResult = dbCollection.update(item, toDBObject(bean)); return writeResult.isUpdateOfExisting(); }
From source file:com.sube.daos.mongodb.CardMongoDaoImpl.java
License:Apache License
@Override public Double addToBalance(SubeCard subeCard, Double money) throws InvalidSubeCardException { DBCollection collection = getCardCollection(); BasicDBObject query = getCardQuery(subeCard); Double balance = getBalanceByQuery(query); Double newBalance = balance.doubleValue() + money; BasicDBObject update = new BasicDBObject("$set", new BasicDBObject("balance", newBalance)); collection.update(query, update); return newBalance; }
From source file:com.tengen.Week3Homework1Stream.java
License:Apache License
public static void main(String[] args) throws UnknownHostException { MongoClient client = new MongoClient(); DB database = client.getDB("school"); DBCollection collection = database.getCollection("students"); try (DBCursor students = collection.find()) { students.forEach(student -> { BasicDBList scores = (BasicDBList) student.get("scores"); DBObject worstHomeScoreDoc = scores.stream().map(o -> (DBObject) o) // cast Object to DBObject .filter(o -> o.get("type").equals("homework")) .min((o1, o2) -> ((Double) o1.get("score")).compareTo((Double) o2.get("score"))).get(); scores.remove(worstHomeScoreDoc); collection.update(new BasicDBObject("_id", student.get("_id")), student); });/*from w w w . ja va2s.c om*/ } }
From source file:com.tengen.Week3Hw3_1.java
License:Apache License
public static void main(String[] args) throws UnknownHostException { MongoClient client = new MongoClient(); DB database = client.getDB("school"); DBCollection collection = database.getCollection("students"); DBCursor cursor = collection.find(); int student_id = -1; int count = 0; try {/* w w w . j a va2s . com*/ while (cursor.hasNext()) { DBObject student = cursor.next(); //student.grades; BasicDBObject searchQuery = new BasicDBObject().append("_id", student.get("_id")); BasicDBList scores = (BasicDBList) student.get("scores"); BasicDBObject[] scoresArr = scores.toArray(new BasicDBObject[0]); double score = 100.0; BasicDBObject rem = new BasicDBObject(); for (BasicDBObject dbObj : scoresArr) { String type = dbObj.get("type").toString(); if (type.equals("homework")) { double s = Double.parseDouble(dbObj.get("score").toString()); if (score > s) { score = s; rem = dbObj; } } } BasicDBObject update = new BasicDBObject("scores", rem); collection.update(searchQuery, new BasicDBObject("$pull", update)); } } finally { cursor.close(); } }
From source file:com.tml.pathummoto.Dao.CustomDao.java
public void updateCustomer(Customer customer) { MongoClient mongoClient = new MongoClient("localhost", 27017); // Now connect to your databases DB db = mongoClient.getDB("pathumdb"); System.out.println("Connect to database successfully"); DBCollection coll = db.getCollection("customer"); System.out.println("Collection user selected successfully"); BasicDBObject whereQuery = new BasicDBObject(); whereQuery.put("_id", customer.getVehicleNo()); DBCursor cursor = coll.find(whereQuery); System.out.println(customer.getVehicleNo()); DBObject query = new BasicDBObject("_id", customer.getVehicleNo()); DBObject update = new BasicDBObject(); update.put("$set", new BasicDBObject("payment", customer.getPayment()) .append("freeServiceNo", customer.getFreeServiceNo()).append("serviceNo", customer.getServiceNo()) .append("dateOfDelivery", customer.getDateOfDelivery()).append("lastKm", customer.getLastKm())); WriteResult result = coll.update(query, update); System.out.println(result);//from ww w . java 2s. co m }