List of usage examples for com.mongodb DBCollection save
public WriteResult save(final DBObject document)
From source file:me.philnate.textmanager.updates.Update12_12.java
License:Open Source License
private void updateNames() { LOG.info("Going to separate Customer name into first and LastName"); final String contact = "contactName"; DBCollection customers = ds.getCollection(Customer.class); for (DBObject customer : customers.find()) { if (customer.containsField(contact)) { String name = (String) customer.get(contact); LOG.info(String.format("Customer with contactName '%s' found", name)); if (name.contains(" ")) { // customer has first and last name set so lets split it. We // consider that the first name has only one name and not // multiple String[] parts = name.split(" ", 2); customer.put("firstName", parts[0]); customer.put("lastName", parts[1]); LOG.info(String.format("Updated customer %s, firstName: '%s', lastName: '%s'", customer.get("_id"), parts[0], parts[1])); } else { customer.put("firstName", ""); customer.put("lastName", name); }/* w w w. j ava 2 s. com*/ } else { customer.put("firstName", ""); customer.put("lastName", ""); LOG.warn(String.format("Customer %s has no contactName", customer.get("_id"))); } // finally remove the legacy contactName and save the customer customer.removeField(contact); customers.save(customer); } }
From source file:mini_mirc_server.miniIRCHandler.java
private int UpdateLastActive(String username) { int ret = 0;/*from ww w . j av a 2s . c om*/ try { MongoClient mongoClient = new MongoClient(); DB db = mongoClient.getDB("mirc"); DBCollection coll = db.getCollection("activeUser"); BasicDBObject query = new BasicDBObject("username", username); DBCursor cursor = coll.find(query); try { if (cursor.hasNext()) { DBObject temp = cursor.next(); java.util.Date date = new java.util.Date(); temp.put("timestamp", date); coll.save(temp); } else { java.util.Date date = new java.util.Date(); BasicDBObject doc = new BasicDBObject("username", username).append("timestamp", date); coll.insert(doc); System.out.println(username + " online !"); } } finally { cursor.close(); } } catch (UnknownHostException ex) { Logger.getLogger(miniIRCHandler.class.getName()).log(Level.SEVERE, null, ex); } return 0; }
From source file:mongodb.JavaDocSave.java
public static void saveBlueDoc(DBCollection collection) { BasicDBObject query = new BasicDBObject("word", "ocean"); DBObject word = collection.findOne(query); word.put("category", "blue"); WriteResult result = collection.save(word); System.out.println("Update Result: \n" + result.toString()); }
From source file:mongodb.JavaDocSave.java
public static void resetDoc(DBCollection collection) { BasicDBObject query = new BasicDBObject("word", "ocean"); DBObject word = collection.findOne(query); word.put("category", ""); WriteResult result = collection.save(word); System.out.println("Update Result: \n" + result.toString()); }
From source file:net.autosauler.ballance.server.model.AbstractStructuredData.java
License:Apache License
/** * Update record./*w ww . j av a 2 s .com*/ * * @return true, if successful */ private boolean updateRecord() { boolean result = false; DBObject doc = getRecord(getNumber()); if (doc == null) { result = createRecord(); } else { onUpdate(); doc = store(doc); DB db = Database.get(getDomain()); if (db != null) { Database.retain(); DBCollection coll = db.getCollection(getTableName()); coll.save(doc); Database.release(); result = true; } } return result; }
From source file:net.autosauler.ballance.server.model.GlobalSettings.java
License:Apache License
/** * Save changed values./*from ww w .j a v a 2 s .c o m*/ */ public void save() { if (changed) { DBObject myDoc = null; Database.retain(); DB db = Database.get(domain); if (db != null) { DBCollection coll = db.getCollection(SETTINGSTABLE); Set<String> keys = values.keySet(); Iterator<String> i = keys.iterator(); while (i.hasNext()) { String name = i.next(); String val = values.get(name); BasicDBObject query = new BasicDBObject(); query.put("name", name); query.put("domain", domain); myDoc = coll.findOne(query); if (myDoc != null) { if (!val.equals(myDoc.get("val"))) { myDoc.put("val", val); coll.save(myDoc); } } else { myDoc = new BasicDBObject(); myDoc.put("name", name); myDoc.put("domain", domain); myDoc.put("val", val); coll.insert(myDoc); } } changed = false; } Database.release(); } }
From source file:net.autosauler.ballance.server.model.Scripts.java
License:Apache License
/** * Sets the text.//from www . j a v a 2s.c om * * @param txt * the txt * @param andstore * the andstore */ public void setText(String txt, boolean andstore) { text = txt; if (andstore) { DBObject doc = null; DB db = Database.get(domain); if (db != null) { Database.retain(); DBCollection coll = db.getCollection(TABLENAME); BasicDBObject query = new BasicDBObject(); query.put("domain", domain); query.put("name", name); doc = coll.findOne(query); if (doc != null) { doc.put("text", text); coll.save(doc); } else { doc = new BasicDBObject(); doc.put("domain", domain); doc.put("name", name); doc.put("text", text); coll.insert(doc); } Database.release(); initVM(null); } } }
From source file:net.bunselmeyer.mongo.migrate.Migration.java
License:Apache License
protected void renameField(DB db, String collectionName, String from, String to) { DBCollection collection = db.getCollection(collectionName); DBObject query = new BasicDBObject(from, new BasicDBObject("$exists", true)); DBCursor dbObjects = collection.find(query); for (DBObject dbObject : dbObjects) { dbObject.put(to, dbObject.get(from)); dbObject.removeField(from);/* www.j av a 2s . c om*/ collection.save(dbObject); } }
From source file:net.jurre.edutil.persistence.MongoDB.java
License:Open Source License
private boolean updateStationMarketData(String stationName, String systemName, Object marketData) { logger.debug("updated market data for " + stationName + " in " + systemName); DBCollection stations = this.db.getCollection(STATIONS_COLLECTION); BasicDBObject filter = new BasicDBObject(); filter.put("system_id", getSystemID(systemName)); filter.append("name", stationName); DBObject station = stations.findOne(filter); if (station != null) { station.put("commodities", marketData); stations.save(station); logger.info("updated market info for Station : " + stationName + " in System: " + systemName); return true; } else {//from ww w . j a va2 s.c o m logger.error("Unknown station " + stationName + " in " + systemName); // look up possible alternatives , for example fulltext search of stations in system high scorer } return false; }
From source file:net.jurre.edutil.persistence.MongoDB.java
License:Open Source License
private void saveData(String data, String collectionName) { BasicDBObject dataObj = (BasicDBObject) JSON.parse(data); DBCollection collection = this.db.getCollection(collectionName); if (!dataObj.containsField("_id")) { if (dataObj.containsField("id")) dataObj.append("_id", dataObj.get("id")); }// w w w . j a va2s . co m /* BasicDBObject search = new BasicDBObject(); search.put("name", dataObj.getString("name")); DBObject lookup = collection.findOne(search); if ( lookup != null){ dataObj.append("_id", lookup.get("_id")); logger.debug("Found id adding to object"); } */ collection.save(dataObj); }