Example usage for com.mongodb DBCollection remove

List of usage examples for com.mongodb DBCollection remove

Introduction

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

Prototype

public WriteResult remove(final DBObject query) 

Source Link

Document

Remove documents from a collection.

Usage

From source file:usermanager.Main2.java

private void btnDeleteUserActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteUserActionPerformed
    // TODO add your handling code here:
    // Get connection to the Collection
    DB userDB = DBManager.getDatabase();
    DBCollection col = userDB.getCollection("user");

    try {/*from  w w w  .j  a va 2  s . c o m*/
        BasicDBObject searchQuery = new BasicDBObject();
        searchQuery.put("_id", Integer.parseInt(txtId.getText()));
        col.remove(searchQuery);
        System.out.println("Deleted...");
    } catch (MongoException e) {
        e.printStackTrace();
    }

    // Empty the form
    txtId.setText("");
    txtFirstName.setText("");
    txtLastName.setText("");
    txtEmail.setText("");
}

From source file:v7db.files.mongodb.Vermongo.java

License:Open Source License

/**
 * deletes the object without checking for conflicts. An existing version is
 * moved to the shadow collection, along with a dummy version to mark the
 * deletion. This dummy version can contain optional meta-data (such as who
 * deleted the object, and when).//from   w w w .ja  v  a2s .  c o  m
 */
static DBObject remove(DBCollection collection, Object id, BSONObject metaData) {
    DBObject base = collection.findOne(new BasicDBObject("_id", id));
    if (base == null)
        return null;

    // copy to shadow
    DBCollection shadow = getShadowCollection(collection);
    int version = getVersion(base);
    BasicDBObject revId = new BasicDBObject("_id", getId(base)).append(_VERSION, version);
    base.put("_id", revId);
    WriteResult r = shadow.insert(base, WriteConcern.SAFE);

    // TODO: if already there, no error
    r.getLastError().throwOnError();

    // add the dummy version
    BasicDBObject dummy = new BasicDBObject("_id", revId.append(_VERSION, version + 1)).append(_VERSION,
            "deleted:" + (version + 1));
    if (metaData != null)
        dummy.putAll(metaData);
    r = shadow.insert(dummy, WriteConcern.SAFE);
    // TODO: if already there, no error
    r.getLastError().throwOnError();

    collection.remove(new BasicDBObject("_id", id));
    return base;

}

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

License:Apache License

@DELETE
@Path("/delete/{product}")
@Produces("application/json")
public Response softDeleteEntireProduct(@PathParam("product") final String product,
        @Context final HttpServletRequest req, @Context final HttpServletResponse response) throws IOException {

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

    final BasicDBObject query = new BasicDBObject("coordinates.product", product);

    final DBCursor cursor = collection.find(query);
    DBObject doc;//from w w w . j  a  v a 2 s.c  o m

    while (cursor.hasNext()) {
        doc = cursor.next();
        //kill the old id
        doc.removeField("_id");
        try {
            targetCollection.insert(doc);
        } catch (Throwable e) {
            return Response.status(500).build();
        }
    }

    collection.remove(query);

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

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

License:Apache License

@DELETE
@Path("/delete/{product}/{version}")
@Produces("application/json")
public Response softDeleteSingleVersion(@PathParam("product") final String product,
        @PathParam("version") final String version, @Context final HttpServletRequest req,
        @Context final HttpServletResponse response) throws IOException {

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

    final Pattern productReg = java.util.regex.Pattern.compile("^" + product + "/" + version + "$");
    final BasicDBObject query = new BasicDBObject("_id", productReg);

    final DBCursor cursor = collection.find(query);
    DBObject doc;//from  ww  w .j av  a 2s  . c  om

    while (cursor.hasNext()) {
        doc = cursor.next();
        //kill the old id
        doc.removeField("_id");
        try {
            targetCollection.insert(doc);
        } catch (Throwable e) {
            return Response.status(500).build();
        }
    }

    collection.remove(query);

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

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

License:Apache License

@PUT
@Consumes(MediaType.APPLICATION_JSON)//from   w w  w .  j  a v a2 s . c  o  m
@Path("/{product}")
@Produces("application/json")
public Response renameProduct(@PathParam("product") final String product, final Product renameObject) {

    final DB db = this.client.getDB("bdd");
    final List<DBCollection> collections = Arrays.asList(db.getCollection("summary"),
            db.getCollection("features"), db.getCollection("reportStats"), db.getCollection("testingTips"));

    final Pattern productReg = Pattern.compile("^" + product + "/");
    final BasicDBObject query = new BasicDBObject("_id", productReg);

    //Before anything lets check the new name isn't already in use

    final BasicDBObject existsQuery = new BasicDBObject("_id", Pattern.compile("^" + renameObject.name + "/"));
    final int summaryCount = db.getCollection("summary").find(existsQuery).count();
    final int delCount = db.getCollection("deletedSummary").find(existsQuery).count();

    if (delCount + summaryCount != 0) {
        throw new WebApplicationException();
    }

    //We need to rename the product everywhere
    //First up are all the collection with the product in the _id attribute
    for (DBCollection collectioni : collections) {
        DBCursor cursor = collectioni.find(query);
        while (cursor.hasNext()) {
            DBObject doc = cursor.next();
            String id = (String) doc.get("_id");
            doc = renameDoc(product, renameObject.name, doc);
            collectioni.insert(doc);
            collectioni.remove(new BasicDBObject("_id", id));
        }
    }

    //Then we deal with the environments collection where only the coordinates.product is set
    final DBCollection[] noIDCollections = { db.getCollection("environments"),
            db.getCollection("deletedSummary") };
    final BasicDBObject enviroQuery = new BasicDBObject("coordinates.product", product);

    for (int i = 0; i < noIDCollections.length; i++) {
        final DBCursor enviroCursor = noIDCollections[i].find(enviroQuery);

        while (enviroCursor.hasNext()) {
            DBObject doc = enviroCursor.next();
            DBObject coordinates = (DBObject) doc.get("coordinates");
            coordinates.put("product", renameObject.name);
            DBObject updateDoc = new BasicDBObject("$set", new BasicDBObject("coordinates", coordinates));
            noIDCollections[i].update(new BasicDBObject("_id", doc.get("_id")), updateDoc);
        }
    }

    //Then we correct the name in any users favourites object
    final DBCollection userCollection = db.getCollection("users");
    final BasicDBObject favouriteQuery = new BasicDBObject("favourites." + product,
            new BasicDBObject("$exists", true));
    final DBCursor users = userCollection.find(favouriteQuery);

    while (users.hasNext()) {
        DBObject doc = users.next();
        DBObject favs = (DBObject) doc.get("favourites");
        favs.put(renameObject.name, favs.get(product));
        BasicDBObject updateDoc = new BasicDBObject("$set", new BasicDBObject("favourites", favs));
        updateDoc.put("$unset", new BasicDBObject(product, ""));
        userCollection.update(new BasicDBObject("_id", doc.get("_id")), updateDoc);
    }

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

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

License:Apache License

protected void updateStatsDocument(final DB bdd, final Coordinates coordinates, final BasicDBList features) {
    // product and version are redundant for search, but ensure they're populated if the upsert results in an insert.
    final DBCollection statsCollection = bdd.getCollection("reportStats");
    final String id = coordinates.getProduct() + "/" + coordinates.getVersionString() + "/"
            + coordinates.getBuild();/*from   www .jav  a 2 s .  c om*/
    statsCollection.remove(new BasicDBObject("_id", id));
    final BasicDBObject stats = new BasicDBObject("coordinates", coordinates.getReportCoordinates());
    stats.put("_id", id);
    final BasicDBObject summary = new BasicDBObject();
    stats.put("summary", summary);
    final BasicDBObject feature = new BasicDBObject();
    stats.put("feature", feature);
    for (final Object ob : features) {
        final BasicDBList scenarios = (BasicDBList) ((DBObject) ob).get("elements");
        if (scenarios != null) {
            for (final Object o : scenarios) {
                final String status = StatusHelper.getFinalScenarioStatus((DBObject) o, false).getTextName();
                final Integer statusCounter = (Integer) summary.get(status);
                if (statusCounter == null) {
                    summary.put(status, 1);
                } else {
                    summary.put(status, statusCounter + 1);
                }
            }
        }
    }
    statsCollection.save(stats);
}