Example usage for com.mongodb DBCollection findAndModify

List of usage examples for com.mongodb DBCollection findAndModify

Introduction

In this page you can find the example usage for com.mongodb DBCollection findAndModify.

Prototype

@Nullable
public DBObject findAndModify(@Nullable final DBObject query, @Nullable final DBObject fields,
        @Nullable final DBObject sort, final boolean remove, @Nullable final DBObject update,
        final boolean returnNew, final boolean upsert) 

Source Link

Document

Atomically modify and return a single document.

Usage

From source file:pl.nask.hsn2.os.MongoConnector.java

License:Open Source License

public long getNextObjId() {
    DBCollection counters = db.getCollection("counters");
    BasicDBObject result = (BasicDBObject) counters.findAndModify(counterQuery, new BasicDBObject(),
            new BasicDBObject(), false, counterUpdate, true, true);
    return result.getLong("counter");
}

From source file:rapture.sheet.mongodb.MongoCellStore.java

License:Open Source License

private void putCell(String sheetName, int row, int column, String value, int dimension, long epoch) {
    DBCollection collection = MongoDBFactory.getDB(instanceName).getCollection(tableName);
    BasicDBObject query = new BasicDBObject();
    query.put(KEY, sheetName);//from  w  w w.j  ava  2s.  co  m
    query.put(ROW, row);
    query.put(COL, column);
    query.put(DIM, dimension);

    BasicDBObject toPut = new BasicDBObject();
    toPut.put(KEY, sheetName);
    toPut.put(ROW, row);
    toPut.put(COL, column);
    toPut.put(DIM, dimension);
    toPut.put(VALUE, value);
    toPut.put(EPOCH, epoch);
    collection.findAndModify(query, null, null, false, toPut, false, true);
}

From source file:xbdd.webapp.resource.feature.Favourites.java

License:Apache License

public void setFavouriteStateOfProduct(final String product, final boolean state,
        final HttpServletRequest req) {

    final DB db = this.client.getDB("bdd");
    final DBCollection collection = db.getCollection("users");

    final BasicDBObject user = new BasicDBObject();
    user.put("user_id", req.getRemoteUser());

    final DBObject blank = new BasicDBObject();
    collection.findAndModify(user, blank, blank, false, new BasicDBObject("$set", user), true, true);

    //User exists
    final DBObject favourites = new BasicDBObject("favourites." + product, state);
    final DBObject update = new BasicDBObject("$set", favourites);
    collection.update(user, update);/*w ww.  ja v  a2s .c  o m*/
}

From source file:xbdd.webapp.resource.feature.Favourites.java

License:Apache License

@GET
@Path("/")
@Produces("application/json")
public DBObject getSummaryOfAllReports(@Context final HttpServletRequest req) {
    final DB db = this.client.getDB("bdd");
    final DBCollection collection = db.getCollection("summary");
    final DBCollection usersCollection = db.getCollection("users");

    final BasicDBObject user = new BasicDBObject();
    user.put("user_id", req.getRemoteUser());

    final DBObject blank = new BasicDBObject();
    DBObject uDoc = usersCollection.findAndModify(user, blank, blank, false,
            new BasicDBObject("$setOnInsert", new BasicDBObject("favourites", new BasicDBObject())), true,
            true);//from ww  w.  j a v  a2  s .  com
    DBObject userFavourites;

    if (uDoc.containsField("favourites")) {
        userFavourites = (DBObject) uDoc.get("favourites");
    } else {
        userFavourites = new BasicDBObject();
    }

    final DBCursor cursor = collection.find();

    try {
        final BasicDBList returns = new BasicDBList();
        DBObject doc;

        while (cursor.hasNext()) {
            doc = cursor.next();
            String product = ((String) ((DBObject) doc.get("coordinates")).get("product"));
            if (userFavourites.containsField(product) && (boolean) userFavourites.get(product)) {
                doc.put("favourite", userFavourites.get(product));
                returns.add(doc);
            }
        }

        return returns;
    } finally {
        cursor.close();
    }
}

From source file:xbdd.webapp.resource.feature.Recents.java

License:Apache License

/**
 * Uses the '.+' regexp on featureId to allow for symbols such as slashes in the id
 * //from  w  ww  . j av a2s. c o  m
 * @param String id The featureId to get the history for
 * @return Response Either a 200 response or a 500
 */
@PUT
@Path("/feature/{product}/{major}.{minor}.{servicePack}/{build}/{id:.+}")
@Produces("application/json")
public Response addFeatureToRecents(@QueryParam("name") final String featureName,
        @BeanParam Coordinates coordinates, @PathParam("id") final String featureID,
        @Context final HttpServletRequest req) {

    final BasicDBObject featureDetails = new BasicDBObject("name", featureName);
    featureDetails.put("product", coordinates.getProduct());
    featureDetails.put("version", coordinates.getVersionString());
    featureDetails.put("build", coordinates.getBuild());
    featureDetails.put("id", featureID);

    final DB db = this.client.getDB("bdd");
    final DBCollection collection = db.getCollection("users");

    final BasicDBObject user = new BasicDBObject();
    user.put("user_id", req.getRemoteUser());

    final DBObject blank = new BasicDBObject();
    DBObject doc = collection.findAndModify(user, blank, blank, false, new BasicDBObject("$set", user), true,
            true);

    if (doc.containsField("recentFeatures")) {
        BasicDBList featureArray = (BasicDBList) doc.get("recentFeatures");
        if (featureArray.contains(featureDetails)) {
            featureArray.remove(featureDetails);
            featureArray.add(featureDetails);
            collection.update(user,
                    new BasicDBObject("$set", new BasicDBObject("recentFeatures", featureArray)));
        } else {
            if (featureArray.size() >= 5) {
                collection.update(user, new BasicDBObject("$pop", new BasicDBObject("recentFeatures", "-1")));
            }
            collection.update(user,
                    new BasicDBObject("$addToSet", new BasicDBObject("recentFeatures", featureDetails)));
        }
    } else {
        collection.update(user,
                new BasicDBObject("$addToSet", new BasicDBObject("recentFeatures", featureDetails)));
    }

    return Response.ok().build();
}

From source file:xbdd.webapp.resource.feature.Recents.java

License:Apache License

@PUT
@Path("/build/{product}/{major}.{minor}.{servicePack}/{build}")
@Produces("application/json")
public Response addBuildToRecents(@BeanParam Coordinates coordinates, @Context final HttpServletRequest req) {

    DBObject buildCoords = coordinates.getReportCoordinates();

    final DB db = this.client.getDB("bdd");
    final DBCollection collection = db.getCollection("users");

    final BasicDBObject user = new BasicDBObject();
    user.put("user_id", req.getRemoteUser());

    final DBObject blank = new BasicDBObject();
    DBObject doc = collection.findAndModify(user, blank, blank, false, new BasicDBObject("$set", user), true,
            true);/* w  ww  .j a  v  a 2 s.  c o  m*/

    if (doc.containsField("recentBuilds")) {
        BasicDBList buildArray = (BasicDBList) doc.get("recentBuilds");
        if (buildArray.contains(buildCoords)) {
            //BasicDBObject toMove = (BasicDBObject) featureArray.get(featureArray.indexOf(featureDetails));
            buildArray.remove(buildCoords);
            buildArray.add(buildCoords);
            collection.update(user, new BasicDBObject("$set", new BasicDBObject("recentBuilds", buildArray)));
        } else {
            if (buildArray.size() >= 5) {
                collection.update(user, new BasicDBObject("$pop", new BasicDBObject("recentBuilds", "-1")));
            }
            collection.update(user,
                    new BasicDBObject("$addToSet", new BasicDBObject("recentBuilds", buildCoords)));
        }
    } else {
        collection.update(user, new BasicDBObject("$addToSet", new BasicDBObject("recentBuilds", buildCoords)));
    }

    return Response.ok().build();
}