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:uuidupdater.DatabaseTool.java
private void updateProfiles(HashMap<String, String> profiles, String playerfield, String uuidfield, boolean remove) { DBCollection collection = mongo.getDB(db).getCollection(this.collection); boolean isList = collection.findOne(new BasicDBObject(playerfield, new BasicDBObject("$exists", true))) .get(playerfield) instanceof BasicDBList; if (!isList) { for (String name : profiles.keySet()) { for (ObjectId _id : occurencies.get(name)) { BasicDBObject query = new BasicDBObject(); query.put("_id", _id); BasicDBObject updateObj = new BasicDBObject(); updateObj.put("$set", new BasicDBObject(uuidfield, profiles.get(name))); if (remove) updateObj.put("$unset", new BasicDBObject(playerfield, "")); collection.update(query, updateObj, false, false); }/*from www . ja v a2s. c o m*/ } } else { for (ObjectId oid : players.keySet()) { BasicDBObject query = new BasicDBObject(); query.put("_id", oid); BasicDBObject updateObj = new BasicDBObject(); BasicDBList updateList = new BasicDBList(); for (String name : players.get(oid)) { updateList.add(profiles.get(name)); } updateObj.put("$set", new BasicDBObject(uuidfield, updateList)); if (remove) updateObj.put("$unset", new BasicDBObject(playerfield, "")); collection.update(query, updateObj, false, false); } } }
From source file:xbdd.webapp.resource.feature.Report.java
License:Apache License
protected void updateSummaryDocument(final DB bdd, final Coordinates coordinates) { // product and version are redundant for search, but ensure they're populated if the upsert results in an insert. final DBObject summaryQuery = new BasicDBObject("_id", coordinates.getProduct() + "/" + coordinates.getVersionString()).append("coordinates", coordinates.getObject(Field.PRODUCT, Field.VERSION)); final DBCollection summary = bdd.getCollection("summary"); final DBObject summaryObject = summary.findOne(summaryQuery); if (summaryObject != null) { // lookup the summary document @SuppressWarnings("unchecked") final List<String> buildArray = (List<String>) summaryObject.get("builds"); if (!buildArray.contains(coordinates.getBuild())) { // only update it if this build hasn't been added to it before. // Update index document version. summary.update(summaryQuery, new BasicDBObject("$push", new BasicDBObject("builds", coordinates.getBuild())), true, false);/*from w w w. ja v a 2s . c om*/ } } else {// if the report doesn't already exist... then add it. summary.update(summaryQuery, new BasicDBObject("$push", new BasicDBObject("builds", coordinates.getBuild())), true, false); } }
From source file:xbdd.webapp.resource.presence.Presence.java
License:Apache License
@POST @Path("/{product}/{major}.{minor}.{servicePack}/{build}/{featureId}") public DBObject addPresence(@BeanParam final Coordinates coordinates, @PathParam("featureId") final String featureId, @Context final HttpServletRequest req) { try {/* ww w.j av a 2 s . c o m*/ final DB db = this.client.getDB("bdd"); final DBCollection collection = db.getCollection("presence"); final BasicDBObject query = new BasicDBObject("coordinates", coordinates.getObject(Field.PRODUCT, Field.VERSION, Field.BUILD).append("featureId", featureId)) .append("_id", coordinates.getProduct() + "/" + coordinates.getVersionString() + "/" + coordinates.getBuild() + "/" + featureId); final Date now = Calendar.getInstance().getTime(); collection.update(query, new BasicDBObject("$set", new BasicDBObject("users." + req.getRemoteUser(), now).append("lastUpdated", now)), true, false); final DBObject newPresence = collection.findOne(query); newPresence.put("currentUser", req.getRemoteUser()); return newPresence; } catch (final Throwable th) { th.printStackTrace(); return null; } }
From source file:xbdd.webapp.resource.presence.Presence.java
License:Apache License
@DELETE @Path("/{product}/{major}.{minor}.{servicePack}/{build}/{featureId}") public DBObject deletePresence(@BeanParam final Coordinates coordinates, @PathParam("featureId") final String featureId, @Context final HttpServletRequest req) { try {/* www . j a va 2 s . c o m*/ final DB db = this.client.getDB("bdd"); final DBCollection collection = db.getCollection("presence"); final BasicDBObject query = new BasicDBObject("coordinates", coordinates.getObject(Field.PRODUCT, Field.VERSION, Field.BUILD).append("featureId", featureId)) .append("_id", coordinates.getProduct() + "/" + coordinates.getVersionString() + "/" + coordinates.getBuild() + "/" + featureId); collection.update(query, new BasicDBObject("$unset", new BasicDBObject("users." + req.getRemoteUser(), 1)), true, false); final DBObject newPresence = collection.findOne(query); newPresence.put("currentUser", req.getRemoteUser()); return newPresence; } catch (final Throwable th) { th.printStackTrace(); return null; } }