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:com.aankam.servlet.MongoCrudServlet.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//ww  w. j  a  v a  2s.  c o m
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    MongoClientURI uri = new MongoClientURI("mongodb://Username:Pasword@ds061721.mongolab.com:61721/cs612");
    MongoClient client = new MongoClient(uri);
    DB db = client.getDB(uri.getDatabase());

    response.setContentType("text/html;charset=UTF-8");

    String serialNo = request.getParameter("serialNo");
    String name = request.getParameter("name");
    String emailId = request.getParameter("emailId");
    String operation = request.getParameter("operation");

    BasicDBObject customer;
    BasicDBObject updateQuery;
    BasicDBObject deleteQuery;
    BasicDBObject searchQuery;
    DBCollection customersCollection;
    customersCollection = db.getCollection("Customers");
    DBObject searchResult = null;
    boolean flag = false;
    DBCursor cursor;
    switch (operation) {
    case "create":
        customer = new BasicDBObject();
        customer.put("serialNo", serialNo);
        customer.put("name", name);
        customer.put("emailId", emailId);
        customersCollection.insert(customer);
        break;
    case "update":
        updateQuery = new BasicDBObject();
        updateQuery.append("serialNo", serialNo);
        cursor = customersCollection.find(updateQuery);
        customer = new BasicDBObject();
        customer.put("serialNo", serialNo);
        customer.put("name", name);
        customer.put("emailId", emailId);
        if (cursor.hasNext()) {
            customersCollection.update(updateQuery, customer);
            flag = true;
        }

        break;
    case "delete":
        deleteQuery = new BasicDBObject();
        deleteQuery.append("serialNo", serialNo);
        customersCollection.remove(deleteQuery);
        break;
    case "search":
        searchQuery = new BasicDBObject();
        searchQuery.append("serialNo", serialNo);
        cursor = customersCollection.find(searchQuery);
        while (cursor.hasNext()) {
            searchResult = cursor.next();
        }
        break;
    default:
        break;

    }
    try (PrintWriter out = response.getWriter()) {
        /* TODO output your page here. You may use following sample code. */
        out.println("<!DOCTYPE html>");
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet MongoCrudServlet</title>");
        out.println("</head>");
        out.println("<body>");

        switch (operation) {
        case "create":
            out.println("<h3>Customer was successfully created.</h3>");
            break;
        case "update":
            if (flag == true) {
                out.println("<h3>Customer was successfully updated.</h3>");
            } else {
                out.println("<h3>Customer was not found to update.</h3>");
            }

            break;
        case "delete":
            out.println("<h3>Customer was successfully deleted.</h3>");
            break;
        case "search":
            if (searchResult != null) {
                out.println("<h3>The following customer was found:</h3>");
                out.println("<h4>Serial No :" + searchResult.get("serialNo") + "</h4>");
                out.println("<h4>Name :" + searchResult.get("name") + "</h4>");
                out.println("<h4>Email Id :" + searchResult.get("emailId") + "</h4>");
            } else {
                out.println("<h3>Customer not found.</h3>");
            }
            break;
        default:
            break;
        }
        out.println("<a href=\"index.html\">Back to Main Form</a>");
        out.println("</body>");
        out.println("</html>");
    }
}

From source file:com.aperigeek.dropvault.web.dao.MongoFileService.java

License:Open Source License

public void delete(String username, String password, Resource resource) {
    DBCollection files = mongo.getDataBase().getCollection("files");
    DBCollection contents = mongo.getDataBase().getCollection("contents");

    for (Resource child : getChildren(resource)) {
        delete(username, password, child);
    }//  www  . jav  a2  s . c  o m

    DBObject filter = new BasicDBObject("_id", resource.getId());

    DBObject current = files.findOne(filter);
    files.update(new BasicDBObject("_id", (ObjectId) current.get("parent")),
            new BasicDBObject("$set", new BasicDBObject("modificationDate", new Date())));

    files.remove(filter);
    contents.remove(new BasicDBObject("resource", resource.getId()));

    try {
        indexService.remove(username, password, resource.getId().toString());
    } catch (IndexException ex) {
        Logger.getLogger(MongoFileService.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:com.apifest.oauth20.MongoDBManager.java

License:Apache License

@Override
public boolean deleteScope(String scopeName) {
    DBCollection coll = db.getCollection(SCOPE_COLLECTION_NAME);
    BasicDBObject query = new BasicDBObject(ID_NAME, scopeName);
    WriteResult result = coll.remove(query);
    return (result.getN() == 1) ? true : false;
}

From source file:com.apifest.oauth20.MongoDBManager.java

License:Apache License

@Override
public void removeAccessToken(String accessToken) {
    BasicDBObject dbObject = new BasicDBObject();
    dbObject.put(ACCESS_TOKEN_ID_NAME, accessToken);
    DBCollection coll = db.getCollection(ACCESS_TOKEN_COLLECTION_NAME);
    List<DBObject> list = coll.find(dbObject).toArray();
    if (list.size() > 0) {
        DBObject newObject = list.get(0);
        coll.findAndModify(dbObject, newObject);
        coll.remove(dbObject);
    }/*from   w w  w  .  j  av  a 2 s . c o  m*/
}

From source file:com.apifest.oauth20.persistence.mongodb.MongoDBManager.java

License:Apache License

@Override
public boolean deleteClientApp(String clientId) {
    DBCollection coll = db.getCollection(CLIENTS_COLLECTION_NAME);
    BasicDBObject query = new BasicDBObject(CLIENTS_ID, clientId);
    WriteResult result = coll.remove(query);
    return (result.getN() == 1);
}

From source file:com.apifest.oauth20.persistence.mongodb.MongoDBManager.java

License:Apache License

@Override
public boolean deleteScope(String scopeName) {
    DBCollection coll = db.getCollection(SCOPE_COLLECTION_NAME);
    BasicDBObject query = new BasicDBObject(CLIENTS_ID, scopeName);
    WriteResult result = coll.remove(query);
    return (result.getN() == 1);
}

From source file:com.apifest.oauth20.persistence.mongodb.MongoDBManager.java

License:Apache License

@Override
public void removeAccessToken(String accessToken) {
    BasicDBObject dbObject = new BasicDBObject(ACCESS_TOKEN_ID, accessToken);
    DBCollection coll = db.getCollection(ACCESS_TOKEN_COLLECTION_NAME);
    coll.remove(dbObject);
}

From source file:com.app.mongoDao.MongoBookDao.java

@Override
public String deleteBook(String book) {
    String status = "";
    try {//from  w  w w . jav a2s . co  m
        DBCollection data = DatabaseConfig.configure();
        BasicDBObject query = new BasicDBObject();
        query.append("bookname", book);
        data.remove(query);
        status = "Book deleted successfully";
    } catch (Exception e) {
        status = "Book not deleted successfully";
        e.printStackTrace();
    }

    return status;
}

From source file:com.aw.util.MongoDbUtil.java

public static WriteResult removeCollection(String collection, DBObject query) {
    DBCollection table = getCollection(defaultDBName, collection);
    return table.remove(query);
}

From source file:com.bbc.remarc.util.DummyDataGenerator.java

License:Apache License

private static void generateVideo(DB db) {

    DBCollection video = db.getCollection("video");

    video.remove(new BasicDBObject());

    /*//from   www  .  j  a  v a2 s . c  om
    video.insert(BasicDBObjectBuilder.start()
    .add("imageUrl", "/remarc_resources/video/cat.jpg")
    .add("theme", "Animals/Pets").add("decade", "1970s")
    .add("id", "cat")
    .add("mp4ContentUrl", "/remarc_resources/video/cat.mp4")
    .add("ogvContentUrl", "/remarc_resources/video/cat.ogv")
    .get());
    */

}