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: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:mongodb.View.java
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed // TODO add your handling code here: String val = jTable1.getModel().getValueAt(jTable1.getSelectedRow(), 0).toString(); try {//from w w w .j a v a 2s. c o m DB userDB = DBManager.getDatabase(); DBCollection col = userDB.getCollection("user"); BasicDBObject query = new BasicDBObject("_id", new BasicDBObject("$eq", Integer.parseInt(val))); BasicDBObject newDocument = new BasicDBObject(); newDocument.put("firstName", jTextField1.getText()); newDocument.put("lastName", jTextField2.getText()); newDocument.put("email", jTextField3.getText()); BasicDBObject updateObj = new BasicDBObject(); updateObj.put("$set", newDocument); col.update(query, updateObj, false, true); JOptionPane.showMessageDialog(rootPane, "User Update SucessFully...!"); } catch (UnknownHostException ex) { Logger.getLogger(View.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:net.luminis.useradmin.mongodb.MongoDBRepositoryStore.java
License:Apache License
@Override public void roleChanged(UserAdminEvent event) { if (UserAdminEvent.ROLE_CHANGED == event.getType()) { // Only the changes are interesting, as the creation and // removal are already caught by #addRole and #removeRole.... Role changedRole = event.getRole(); try {//from w ww . j av a 2 s .c o m DBCollection coll = getCollection(); DBObject query = getTemplateObject(changedRole); DBObject update = m_helper.serializeUpdate(changedRole); WriteResult result = coll.update(query, update, false /* upsert */, false /* multi */); if (result.getLastError() != null && !result.getLastError().ok()) { m_log.log(LogService.LOG_WARNING, "Failed to update changed role: " + changedRole.getName()); } } catch (MongoException e) { m_log.log(LogService.LOG_WARNING, "Failed to update changed role: " + changedRole.getName(), e); } } }
From source file:net.onrc.openvirtex.db.DBManager.java
License:Apache License
/** * Save persistable object obj/*from w ww. j a v a2 s. com*/ * @param obj * @param coll */ public void save(Persistable obj) { BasicDBObject query = new BasicDBObject(); query.putAll(obj.getDBIndex()); BasicDBObject update = new BasicDBObject("$addToSet", new BasicDBObject(obj.getDBKey(), obj.getDBObject())); PrintStream ps = System.err; System.setErr(null); try { DBCollection collection = this.collections.get(obj.getDBName()); collection.update(query, update, true, false); } catch (Exception e) { log.error("Failed to update database: {}", e.getMessage()); } finally { System.setErr(ps); } }
From source file:net.scran24.datastore.mongodb.MongoDbDataStore.java
License:Apache License
@Override public void setSurveyParameters(String survey_id, SurveyParameters newParameters) throws DataStoreException { DBCollection col = getSurveyStateCollection(); final BasicDBObject stateObj = new BasicDBObject(FIELD_SURVEY_ID, survey_id); switch (newParameters.state) { case NOT_INITIALISED: stateObj.append(FIELD_SURVEY_STATE, STATE_NOT_INITIALISED); break;//from w w w .ja v a 2 s .c om case SUSPENDED: stateObj.append(FIELD_SURVEY_STATE, STATE_SUSPENDED); break; case ACTIVE: stateObj.append(FIELD_SURVEY_STATE, STATE_ACTIVE); break; } stateObj.append(FIELD_START_DATE, newParameters.startDate); stateObj.append(FIELD_END_DATE, newParameters.endDate); stateObj.append(FIELD_SCHEME_NAME, newParameters.schemeName); stateObj.append(FIELD_LOCALE_ID, newParameters.locale); stateObj.append(FIELD_ALLOW_GEN_USERS, newParameters.allowGenUsers); stateObj.append(FIELD_SUSPENSION_REASON, newParameters.suspensionReason); newParameters.surveyMonkeyUrl.accept(new Option.SideEffectVisitor<String>() { @Override public void visitSome(String item) { stateObj.append(FIELD_SURVEY_MONKEY_URL, item); } @Override public void visitNone() { if (stateObj.containsField(FIELD_SURVEY_MONKEY_URL)) stateObj.removeField(FIELD_SURVEY_MONKEY_URL); } }); BasicDBObject query = new BasicDBObject(FIELD_SURVEY_ID, survey_id); try { col.update(query, stateObj, true, false); } catch (MongoException e) { throw new DataStoreException(e); } }
From source file:net.scran24.datastore.mongodb.MongoDbDataStore.java
License:Apache License
@Override public void incrementPopularityCount(List<String> foodCodes) throws DataStoreException { DBCollection col = getPopularityCollection(); Set<String> missing = new HashSet<String>(); missing.addAll(foodCodes);//from w ww . j a va2 s . com // FIXME // This query is incorrect: foods may appear multiple times if // foodCodes, // but the counter will only be incremented once, although this is // efficient. // Change this to do that correctly without generating a request for // each // code. DBObject query = new BasicDBObject(FIELD_CODE, new BasicDBObject("$in", foodCodes)); try { DBCursor cursor = col.find(query); for (DBObject o : cursor) missing.remove(o.get(FIELD_CODE)); List<DBObject> newRecords = new ArrayList<DBObject>(); for (String code : missing) newRecords.add(new BasicDBObject(FIELD_CODE, code).append(FIELD_COUNT, 0)); col.insert(newRecords); col.update(query, new BasicDBObject("$inc", new BasicDBObject(FIELD_COUNT, 1)), false, true); } catch (MongoException e) { throw new DataStoreException(e); } }
From source file:net.scran24.datastore.mongodb.MongoDbDataStore.java
License:Apache License
@Override public void setGlobalValue(String name, String value) throws DataStoreException { DBCollection col = getGlobalValuesCollection(); BasicDBObject query = new BasicDBObject("name", name); BasicDBObject update = new BasicDBObject("$set", new BasicDBObject("value", value)); try {// ww w . j a v a 2 s . c om col.update(query, update, true, false); } catch (MongoException e) { throw new DataStoreException(e); } }
From source file:net.scran24.datastore.mongodb.MongoDbDataStore.java
License:Apache License
@Override public void setLastHelpRequestTime(String survey, String username, long time) throws DataStoreException { DBCollection col = getHelpRequestTimeCollection(); BasicDBObject query = new BasicDBObject("user_id", survey + "/" + username); BasicDBObject update = new BasicDBObject("$set", new BasicDBObject("last_help_request_at", time)); try {//from www. j a va 2 s .c om col.update(query, update, true, false); } catch (MongoException e) { throw new DataStoreException(e); } }