List of usage examples for com.mongodb DBCollection update
public WriteResult update(final DBObject query, final DBObject update)
From source file:org.greenmongoquery.db.service.impl.MongoServiceImpl.java
License:Open Source License
public String updateDocument(String json, String dbname, String collectionName, Mongo mongo, String id) { logger.info("start update"); String result = null;//w w w . j av a 2 s. c o m DB db = mongo.getDB(dbname); DBObject orgDbObject = new BasicDBObject("_id", new ObjectId(id)); // id ="509e55b844aefa5410c8e656"; // orgDbObject.put("_id", id); logger.info("update id " + id); logger.info("Object to update" + orgDbObject.toString()); DBCollection coll = db.getCollection(collectionName); DBObject found = coll.findOne(orgDbObject); DBObject dbObject = (DBObject) JSON.parse(json); coll.update(found, dbObject); result = "update document : _id= " + id; return result; }
From source file:org.jboss.narayana.quickstarts.mongodb.simple.AccountManager.java
License:Open Source License
/** * This method credits a specified account by a specified amount. * * It is annotated with @TxCompensate. This annotation specifies the implementation of a compensation handler that * is to be invoked if the work done in this method needs to be compensated later. * * @param account The account to credit// w ww. j a va2 s.com * @param amount The amount to credit the balance by. */ @TxCompensate(UndoCredit.class) public void creditAccount(String account, Double amount) { //High value transfers (over 500) are not allowed with this service if (amount > 500) { //Mark the current transaction as 'compensateOnly'. This ensures that the transaction will fail. compensationManager.setCompensateOnly(); return; } //Set the compensation state. This will be required if the compensation handler is invoked. creditData.setToAccount(account); creditData.setAmount(amount); //Update the account document in MongoDB. DBCollection accounts = database.getCollection("accounts"); accounts.update(new BasicDBObject("name", account), new BasicDBObject("$inc", new BasicDBObject("balance", amount))); }
From source file:org.jboss.narayana.quickstarts.mongodb.simple.AccountManager.java
License:Open Source License
/** * This method debits a specified account by a specified amount. * * It is annotated with @TxCompensate. This annotation specifies the implementation of a compensation handler that * is to be invoked if the work done in this method needs to be compensated later. * * @param account The account to debit/* w w w .j a v a2s . co m*/ * @param amount The amount to debit the balance by. */ @TxCompensate(UndoDebit.class) public void debitAccount(String account, Double amount) { //Set the compensation state. This will be required if the compensation handler is invoked. debitData.setFromAccount(account); debitData.setAmount(amount); //Update the account document in MongoDB. DBCollection accounts = database.getCollection("accounts"); accounts.update(new BasicDBObject("name", account), new BasicDBObject("$inc", new BasicDBObject("balance", -1 * amount))); }
From source file:org.jboss.narayana.quickstarts.mongodb.simple.UndoCredit.java
License:Open Source License
@Override public void compensate() { if (creditData.getToAccount() != null) { System.out.println(/*w ww . ja v a 2 s . c o m*/ "Undoing credit of '" + creditData.getAmount() + "' to '" + creditData.getToAccount() + "'"); //Use the creditData to know which account to lookup and by how much to decrement the balance. DBCollection accounts = database.getCollection("accounts"); accounts.update(new BasicDBObject("name", creditData.getToAccount()), new BasicDBObject("$inc", new BasicDBObject("balance", -1 * creditData.getAmount()))); } }
From source file:org.jboss.narayana.quickstarts.mongodb.simple.UndoDebit.java
License:Open Source License
@Override public void compensate() { System.out.println(// w ww. ja v a 2 s .c om "Undoing debit of '" + debitData.getAmount() + "' from '" + debitData.getFromAccount() + "'"); //Use the debitData to know which account to lookup and by how much to increment the balance. DBCollection accounts = database.getCollection("accounts"); accounts.update(new BasicDBObject("name", debitData.getFromAccount()), new BasicDBObject("$inc", new BasicDBObject("balance", debitData.getAmount()))); }
From source file:org.jivesoftware.openfire.roster.DefaultRosterItemProvider.java
License:Open Source License
public RosterItem createItem(String username, RosterItem item) throws UserAlreadyExistsException { try {/*from w w w . j a va 2 s . co m*/ init(); item.setCurrVersion(System.currentTimeMillis()); DBCollection coll = db.getCollection("gUser"); BasicDBObject doc = new BasicDBObject("name", item.getNickname()) .append("jid", item.getJid().toBareJID()).append("himId", item.getJid().getNode()) .append("sub", item.getSubStatus().getValue()).append("ask", item.getAskStatus().getValue()) .append("recv", item.getRecvStatus().getValue()).append("ver", item.getCurrVersion()) .append("groupName", item.getGroups()); BasicDBObject q = new BasicDBObject("himId", username); DBCursor res = coll.find(q, new BasicDBObject("_id", 0).append("friends", new BasicDBObject("$elemMatch", new BasicDBObject("himId", item.getJid().getNode())))); Iterator<DBObject> iter = res.iterator(); while (iter.hasNext()) { BasicDBList result = (BasicDBList) ((BasicDBObject) iter.next()).get("friends"); if (result != null && result.size() > 0) { new UserAlreadyExistsException(item.getJid().toBareJID()); } } WriteResult writeRes = coll.update(new BasicDBObject("himId", username), new BasicDBObject("$push", new BasicDBObject("friends", doc)).append("$inc", new BasicDBObject("friendsCount", 1))); if (!((Boolean) writeRes.getField("updatedExisting"))) { new UserAlreadyExistsException(item.getJid().toBareJID()); } } catch (Exception e) { Log.warn("Error trying to insert a new row in ofRoster", e); throw new UserAlreadyExistsException(item.getJid().toBareJID()); } return item; }
From source file:org.jivesoftware.openfire.roster.DefaultRosterItemProvider.java
License:Open Source License
public void updateItem(String username, RosterItem item) throws UserNotFoundException { // "UPDATE ofRoster SET sub=?, ask=?, recv=?, nick=?,version=? WHERE rosterID=?" try {// w w w. j a va2s . c o m init(); item.setCurrVersion(System.currentTimeMillis()); DBCollection coll = db.getCollection("gUser"); BasicDBObject doc = new BasicDBObject("friends.$.name", item.getNickname()) .append("friends.$.sub", item.getSubStatus().getValue()) .append("friends.$.ask", item.getAskStatus().getValue()) .append("friends.$.recv", item.getRecvStatus().getValue()) .append("friends.$.ver", item.getCurrVersion()).append("friends.$.groupName", item.getGroups()); BasicDBObject q = new BasicDBObject("himId", username).append("friends.himId", item.getJid().getNode()); WriteResult res = coll.update(q, new BasicDBObject("$set", doc)); if (!((Boolean) res.getField("updatedExisting"))) { Log.warn("Unable to update roster item"); } } catch (Exception e) { Log.warn("Error trying to insert a new row in ofRoster", e); } }
From source file:org.jivesoftware.openfire.roster.DefaultRosterItemProvider.java
License:Open Source License
public void deleteItem(String username, String friendId) { try {// www . j a va 2s. c o m init(); DBCollection coll = db.getCollection("gUser"); BasicDBObject doc = new BasicDBObject("$pull", new BasicDBObject("friends", new BasicDBObject("himId", friendId))); BasicDBObject q = new BasicDBObject("himId", username); WriteResult res = coll.update(q, doc.append("$inc", new BasicDBObject("friendsCount", -1))); if (!((Boolean) res.getField("updatedExisting"))) { Log.warn("Unable to update roster item"); } } catch (Exception e) { Log.warn("Error trying to insert a new row in ofRoster", e); } }
From source file:org.jivesoftware.openfire.roster.HimRosterItemProvider.java
License:Open Source License
public RosterItem createItem(String username, RosterItem item) throws UserAlreadyExistsException { try {// ww w.ja v a 2 s. c om DBCollection coll = db.getCollection("gUser"); BasicDBObject doc = new BasicDBObject("name", item.getNickname()).append("himId", username) .append("sub", item.getSubStatus().getValue()).append("ask", item.getAskStatus().getValue()) .append("recv", item.getRecvStatus().getValue()).append("currVer", item.getCurrVersion()) .append("groupName", item.getGroups()); ; BasicDBObject q = new BasicDBObject("himId", username); // INSERT INTO ofRoster (username, rosterID, jid, sub, ask, recv, nick,version) WriteResult res = coll.update(q, new BasicDBObject("$push", new BasicDBObject("friends", doc))); } catch (Exception e) { Log.warn("Error trying to insert a new row in ofRoster", e); throw new UserAlreadyExistsException(item.getJid().toBareJID()); } return item; }
From source file:org.keycloak.connections.mongo.updater.impl.updates.AbstractMigrateUserFedToComponent.java
License:Apache License
public void portUserFedToComponent(String providerId) { DBCollection realms = db.getCollection("realms"); DBCursor cursor = realms.find();/* w ww . j ava 2 s . com*/ while (cursor.hasNext()) { BasicDBObject realm = (BasicDBObject) cursor.next(); String realmId = realm.getString("_id"); Set<String> removedProviders = new HashSet<>(); BasicDBList componentEntities = (BasicDBList) realm.get("componentEntities"); BasicDBList federationProviders = (BasicDBList) realm.get("userFederationProviders"); for (Object obj : federationProviders) { BasicDBObject fedProvider = (BasicDBObject) obj; if (fedProvider.getString("providerName").equals(providerId)) { String id = fedProvider.getString("id"); removedProviders.add(id); int priority = fedProvider.getInt("priority"); String displayName = fedProvider.getString("displayName"); int fullSyncPeriod = fedProvider.getInt("fullSyncPeriod"); int changedSyncPeriod = fedProvider.getInt("changedSyncPeriod"); int lastSync = fedProvider.getInt("lastSync"); BasicDBObject component = new BasicDBObject(); component.put("id", id); component.put("name", displayName); component.put("providerType", UserStorageProvider.class.getName()); component.put("providerId", providerId); component.put("parentId", realmId); BasicDBObject config = new BasicDBObject(); config.put("priority", Collections.singletonList(Integer.toString(priority))); config.put("fullSyncPeriod", Collections.singletonList(Integer.toString(fullSyncPeriod))); config.put("changedSyncPeriod", Collections.singletonList(Integer.toString(changedSyncPeriod))); config.put("lastSync", Collections.singletonList(Integer.toString(lastSync))); BasicDBObject fedConfig = (BasicDBObject) fedProvider.get("config"); if (fedConfig != null) { for (Map.Entry<String, Object> attr : new HashSet<>(fedConfig.entrySet())) { String attrName = attr.getKey(); String attrValue = attr.getValue().toString(); config.put(attrName, Collections.singletonList(attrValue)); } } component.put("config", config); componentEntities.add(component); } } Iterator<Object> it = federationProviders.iterator(); while (it.hasNext()) { BasicDBObject fedProvider = (BasicDBObject) it.next(); String id = fedProvider.getString("id"); if (removedProviders.contains(id)) { it.remove(); } } realms.update(new BasicDBObject().append("_id", realmId), realm); } }