List of usage examples for com.mongodb DBCollection update
public WriteResult update(final DBObject query, final DBObject update, final boolean upsert, final boolean multi)
From source file:act.installer.reachablesexplorer.ReachablesProjectionUpdate.java
License:Open Source License
public void updateDatabase(DBCollection reachables) { for (String product : products) { // The query object for this product BasicDBObject newProductQuery = new BasicDBObject().append(INCHI_KEY, product); // DB list of the substrates of this projection BasicDBList substrateList = new BasicDBList(); substrateList.addAll(substrates); // DB list of the one RO associated with this projection BasicDBList roList = new BasicDBList(); roList.addAll(ros);/*from w ww .j av a 2 s . co m*/ // The full entry to be added to the product's precursor list BasicDBObject precursorEntry = new BasicDBObject().append(SUBSTRATES_KEY, substrateList).append(RO_KEY, roList); // The command to push the precursor entry onto the precursor list BasicDBObject precursors = new BasicDBObject(); precursors.append("$push", new BasicDBObject(PRECURSOR_KEY, precursorEntry)); // Do the update! reachables.update(newProductQuery, precursors, UPSERT, NO_MULTI); } }
From source file:calliope.core.database.MongoConnection.java
License:Open Source License
/** * PUT a json file to the database//from w w w.j a va2s . c o m * @param collName the name of the collection * @param docID the docid of the resource * @param json the json to put there * @return the server response */ @Override public String putToDb(String collName, String docID, String json) throws DbException { try { DBObject doc = (DBObject) JSON.parse(json); doc.put(JSONKeys.DOCID, docID); connect(); DBCollection coll = getCollectionFromName(collName); DBObject query = new BasicDBObject(JSONKeys.DOCID, docID); WriteResult result = coll.update(query, doc, true, false); //return removeFromDb( path ); return result.toString(); } catch (Exception e) { throw new DbException(e); } }
From source file:calliope.core.database.MongoConnection.java
License:Open Source License
/** * PUT a json file to the database using dbase, docid and version * @param collName the name of the collection * @param dbase the name of the database * @param docid the document identifier/*ww w.j a v a2 s . c o m*/ * @param version the version of the document * @param json the json to put there * @return the server response */ @Override public String putToDb(String collName, String dbase, String docid, String version, String json) throws DbException { try { DBObject doc = (DBObject) JSON.parse(json); doc.put(JSONKeys.DOCID, docid); connect(); DBObject query = getThreeFieldQuery(JSONKeys.DBASE, dbase, JSONKeys.DOCID, docid, JSONKeys.VERSION1, version); DBCollection coll = getCollectionFromName(collName); WriteResult result = coll.update(query, doc, true, false); //return removeFromDb( path ); return result.toString(); } catch (Exception e) { throw new DbException(e); } }
From source file:calliope.core.database.MongoConnection.java
License:Open Source License
/** * PUT a new json file to the database//from w w w. j av a 2 s . co m * @param collName the name of the collection * @param json the json to put there * @return the server response */ @Override public String addToDb(String collName, String json) throws DbException { try { DBObject doc = (DBObject) JSON.parse(json); connect(); DBCollection coll = getCollectionFromName(collName); if (doc.containsField(JSONKeys._ID)) { Object id = doc.get(JSONKeys._ID); DBObject query = new BasicDBObject(JSONKeys._ID, id); if (query != null) { WriteResult result = coll.update(query, doc, true, false); return result.toString(); } else throw new Exception("Failed to update object " + id); } else { WriteResult result = coll.insert(doc, WriteConcern.ACKNOWLEDGED); // return the new document's id ObjectId id = (ObjectId) doc.get("_id"); JSONObject jDoc = (JSONObject) JSONValue.parse(result.toString()); jDoc.put("_id", id.toString()); return jDoc.toJSONString(); } } catch (Exception e) { throw new DbException(e); } }
From source file:calliope.db.MongoConnection.java
License:Open Source License
/** * PUT a json file to the database//from www.java 2s . c om * @param collName the name of the collection * @param docID the docid of the resource * @param json the json to put there * @return the server response */ @Override public String putToDb(String collName, String docID, String json) throws AeseException { try { docIDCheck(collName, docID); DBObject doc = (DBObject) JSON.parse(json); doc.put(JSONKeys.DOCID, docID); connect(); DBCollection coll = getCollectionFromName(collName); DBObject query = new BasicDBObject(JSONKeys.DOCID, docID); WriteResult result = coll.update(query, doc, true, false); //return removeFromDb( path ); return result.toString(); } catch (Exception e) { throw new AeseException(e); } }
From source file:cfel.servlet.ImportCsvServlet.java
License:Open Source License
/** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse * response)/*from www. j av a 2 s.com*/ */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { DBCollection collection = mDS.getCollection("photo"); if (collection == null) { response.sendError(404, "No photo collection"); return; } InputStream is = null; try { is = request.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String line = br.readLine(); // Ignore header while ((line = br.readLine()) != null) { String[] values = getStrings(line); if (values.length == 2) { String portal_image_url = values[0]; String class_id = values[1]; System.out.println("url=" + portal_image_url + ", season=" + class_id); DBObject query = new BasicDBObject("portal_image_url", portal_image_url); DBObject photo = collection.findOne(query); if (photo == null) { continue; } Object guessObj = photo.get("guess"); if (guessObj instanceof List<?>) { System.out.println(guessObj); List<?> guess = (List<?>) guessObj; Object lastGuess = guess.get(guess.size() - 1); if (lastGuess instanceof DBObject && class_id.equals(((DBObject) lastGuess).get("class_id"))) { continue; } } DBObject newGuess = new BasicDBObject(); newGuess.put("updated", new Date()); newGuess.put("class_id", class_id); DBObject update = new BasicDBObject("$push", new BasicDBObject("guess", newGuess)); collection.update(photo, update, true, false); } else { System.err.println("Unknown data: " + line); } } } finally { if (is != null) { is.close(); } } response.setStatus(200); }
From source file:cfel.servlet.ServiceServlet.java
License:Open Source License
/** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse * response)// ww w. j a va2 s .c o m * * Insert a new resource */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String type = request.getParameter("type"); String id = request.getParameter("id"); String data = request.getParameter("data"); System.out.println("doPost: type=" + type + " id=" + id + " data=" + data); if ("file".equals(type)) { // Save a file with id doPut(request, response); return; } DBCollection collection = mDS.getCollection(type); if (collection == null) { response.sendError(HttpServletResponse.SC_BAD_REQUEST, String.format("Unknown collection %s", type)); return; } String action = request.getParameter("action"); if (action != null) { // Manipulate database if ("update".equals(action)) { String query = request.getParameter("query"); String update = request.getParameter("update"); String upsert = request.getParameter("upsert"); String multi = request.getParameter("multi"); DBObject queryObj = null; if (id != null) { queryObj = new BasicDBObject("_id", new ObjectId(id)); } else if (query != null) { queryObj = (DBObject) JSON.parse(query); } DBObject updateObj = update != null ? (DBObject) JSON.parse(update) : null; if (queryObj == null || updateObj == null) { response.sendError(HttpServletResponse.SC_BAD_REQUEST, "No query or update parameters"); return; } sendJSON(collection.update(queryObj, updateObj, "true".equals(upsert), "true".equals(multi)), response); } else { response.sendError(HttpServletResponse.SC_BAD_REQUEST, String.format("Unknown action %s", action)); } return; } if (data == null) { response.sendError(HttpServletResponse.SC_BAD_REQUEST, "No data specified"); return; } // Insert a document DBObject dataObject = (DBObject) JSON.parse(data); Object dataID = dataObject.get("_id"); if (dataID != null && collection.findOne(dataID) != null) { response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Duplicated id"); return; } collection.insert(dataObject); sendJSON(dataObject, response); }
From source file:cfel.servlet.ServiceServlet.java
License:Open Source License
/** * @see HttpServlet#doPut(HttpServletRequest, HttpServletResponse) * //from www . ja v a 2 s . co m * Update a resource with id */ protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String type = request.getParameter("type"); String id = request.getParameter("id"); String data = request.getParameter("data"); System.out.println("doPut: type=" + type + " id=" + id); if ("file".equals(type)) { // Save a file if (id == null) { response.sendError(HttpServletResponse.SC_BAD_REQUEST, "No file id specified"); return; } InputStream is = null; try { mDS.saveFile(id, is = request.getInputStream(), request.getContentType()); } finally { if (is != null) { is.close(); } } return; } DBCollection collection = mDS.getCollection(type); if (collection == null) { response.sendError(HttpServletResponse.SC_BAD_REQUEST, String.format("Unknown collection %s", type)); return; } if (data == null) { response.sendError(HttpServletResponse.SC_BAD_REQUEST, "No data specified"); return; } // Update a document DBObject dataObject = (DBObject) JSON.parse(data); Object idObj = dataObject.get("_id"); // = id in source if (idObj == null && id != null) { idObj = new ObjectId(id); // = id in URI } if (idObj == null) { response.sendError(HttpServletResponse.SC_BAD_REQUEST, "No document id specified"); return; } collection.update(new BasicDBObject("_id", idObj), dataObject, true, false); sendJSON(dataObject, response); }
From source file:co.mcme.themedbuilds.database.DatabaseUtil.java
License:Open Source License
public static void storeUpdatedTheme(Theme toStore) { DBCollection themecollection = ThemedBuildPlugin.getMongoUtil().getThemeCollection(); themecollection.update(new BasicDBObject("_id", toStore.get_id()), toStore.toDBObject(), true, false); }
From source file:co.mcme.themedbuilds.database.DatabaseUtil.java
License:Open Source License
public static void storeLot(Lot toStore) { DBCollection lotcollection = ThemedBuildPlugin.getMongoUtil().getLotCollection(); lotcollection.update(new BasicDBObject("_id", toStore.get_id()), toStore.toDBObject(), true, false); DBCollection playercollection = ThemedBuildPlugin.getMongoUtil().getPlayerCollection(); playercollection.update(new BasicDBObject("uuid", toStore.getOwner().getUniqueId().toString()), new BasicDBObject("$push", new BasicDBObject("lots", toStore.get_id())), true, false); }