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:Vehicle_exit.java

private void Exit_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Exit_buttonActionPerformed
    // TODO add your handling code here:

    try {//from  w w w  . j av  a 2 s . c  o m
        vehno = vehno_combo.getSelectedItem().toString();
        vehno_copy = vehno;
        System.out.println("in button number niald" + vehno);

        MongoClient mc = new MongoClient("localhost", 27017);
        DB db = mc.getDB("parking_system");
        DBCollection collection = db.getCollection("entry_info");

        BasicDBObject query_time = new BasicDBObject();
        BasicDBObject field_time = new BasicDBObject();
        field_time.put("intime", 1);
        DBCursor cursor = collection.find(query_time, field_time);

        BasicDBObject query_token = new BasicDBObject();
        BasicDBObject field_token = new BasicDBObject();
        field_time.put("vehno", 1);
        DBCursor cursor_token = collection.find(query_token, field_token);

        while (cursor.hasNext()) {
            BasicDBObject obj_time = (BasicDBObject) cursor.next();
            in_time_mills = Long.parseLong(obj_time.getString("intime"));

            BasicDBObject obj_token = (BasicDBObject) cursor_token.next();
            //db_token = obj_token.getInt("_id");
            vehno_db = obj_token.getString("vehno");
            System.out.println(vehno_db);
            System.out.println("hello");

            System.out.println("hey ayu");

            DBCollection collection1 = db.getCollection("vehicle_count");
            DBCollection collection2 = db.getCollection("statistics");
            vehno = vehno_copy;
            if (veh_count == 2) {
                //vehno = vehno + "W2";

                if (vehno.equals(vehno_db)) {

                    flag = 1;

                    BasicDBObject deleteObject = new BasicDBObject();
                    //deleteObject.put("_id",Token);
                    deleteObject.put("vehno", vehno);
                    DBCursor findQuery = collection.find(deleteObject);
                    collection.remove(deleteObject);

                    int count_bike;
                    BasicDBObject query = new BasicDBObject();
                    BasicDBObject field = new BasicDBObject();
                    field.put("wheeler_2", 1);
                    DBCursor cursor1 = collection1.find(query, field);

                    BasicDBObject obj = (BasicDBObject) cursor1.next();
                    count_bike = obj.getInt("wheeler_2");

                    System.out.println(count_bike);

                    BasicDBObject searchUpdate = new BasicDBObject();
                    searchUpdate.append("wheeler_2", count_bike);

                    BasicDBObject UpdateObject = new BasicDBObject();
                    UpdateObject.append("$set", new BasicDBObject("wheeler_2", count_bike - 1));

                    collection1.update(searchUpdate, UpdateObject);

                    System.out.println("Checking...");

                    time_diff = exit_time_mills - in_time_mills;
                    time_diff = time_diff / 1000;
                    time_diff = time_diff / 60;
                    System.out.println("difftime" + time_diff);
                    if (time_diff > 60)
                        charges = 20 + (time_diff) / 2;
                    else
                        charges = 20;

                    //updating session...

                    int income_bike;
                    BasicDBObject query2 = new BasicDBObject();
                    BasicDBObject field2 = new BasicDBObject();
                    field2.put("2wheeler_income", 1);
                    DBCursor cursor2 = collection2.find(query2, field2);

                    BasicDBObject obj2 = (BasicDBObject) cursor2.next();
                    income_bike = obj2.getInt("2wheeler_income");
                    System.out.println(income_bike);

                    BasicDBObject searchUpdate2 = new BasicDBObject();
                    searchUpdate2.append("2wheeler_income", income_bike);

                    BasicDBObject UpdateObject2 = new BasicDBObject();
                    UpdateObject2.append("$set", new BasicDBObject("2wheeler_income", income_bike + charges));

                    collection2.update(searchUpdate2, UpdateObject2);

                    JOptionPane.showMessageDialog(null, "Vehicle Out !\n" +
                    /*"In Time : " + in_time_mills + "\n" +*/
                            "Extra Parking Time : " + time_diff + "Min.\n" + "Charges : " + charges);

                    System.out.println("Checked...");

                    setVisible(false);
                    new Entry_OR_Exit().setVisible(true);
                    break;
                }
            }

            else if (veh_count == 4) {

                //vehno = vehno + "W4";

                if (vehno.equals(vehno_db)) {

                    flag = 1;

                    BasicDBObject deleteObject = new BasicDBObject();
                    //deleteObject.put("_id",Token);
                    deleteObject.put("vehno", vehno);
                    DBCursor findQuery = collection.find(deleteObject);
                    collection.remove(deleteObject);

                    int count_car;

                    BasicDBObject query = new BasicDBObject();
                    BasicDBObject field = new BasicDBObject();
                    field.put("wheeler_4", 1);
                    DBCursor cursor1 = collection1.find(query, field);

                    BasicDBObject obj = (BasicDBObject) cursor1.next();
                    count_car = obj.getInt("wheeler_4");
                    System.out.println(count_car);

                    BasicDBObject searchUpdate = new BasicDBObject();
                    searchUpdate.append("wheeler_4", count_car);

                    BasicDBObject UpdateObject = new BasicDBObject();
                    UpdateObject.append("$set", new BasicDBObject("wheeler_4", count_car - 1));

                    collection1.update(searchUpdate, UpdateObject);

                    System.out.println("Checking...2");

                    time_diff = exit_time_mills - in_time_mills;
                    time_diff = time_diff / 1000;
                    time_diff = time_diff / 60;
                    System.out.println("difftime" + time_diff);
                    if (time_diff > 60)
                        charges = 40 + (time_diff) * 3 / 2;
                    else
                        charges = 40;

                    //Session update....

                    int income_car;
                    BasicDBObject query2 = new BasicDBObject();
                    BasicDBObject field2 = new BasicDBObject();
                    field2.put("4wheeler_income", 1);
                    DBCursor cursor2 = collection2.find(query2, field2);

                    BasicDBObject obj2 = (BasicDBObject) cursor2.next();
                    income_car = obj2.getInt("4wheeler_income");
                    System.out.println(income_car);

                    BasicDBObject searchUpdate2 = new BasicDBObject();
                    searchUpdate2.append("4wheeler_income", income_car);

                    BasicDBObject UpdateObject2 = new BasicDBObject();
                    UpdateObject2.append("$set", new BasicDBObject("4wheeler_income", income_car + charges));

                    collection2.update(searchUpdate2, UpdateObject2);

                    JOptionPane.showMessageDialog(null, "Vehicle Out !\n" +
                    /*"In Time : " + in_time_mills + "\n" +*/
                            "Extra Parking Time : " + time_diff + "Min.\n" + "Charges : " + charges);

                    System.out.println("Checking...2");

                    setVisible(false);
                    new Entry_OR_Exit().setVisible(true);
                    break;

                }
            } else {

                //vehno = vehno + "W6";
                if (vehno.equals(vehno_db)) {

                    flag = 1;

                    BasicDBObject deleteObject = new BasicDBObject();
                    //deleteObject.put("_id",Token);
                    deleteObject.put("vehno", vehno);
                    DBCursor findQuery = collection.find(deleteObject);
                    collection.remove(deleteObject);

                    int count_other;

                    BasicDBObject query = new BasicDBObject();
                    BasicDBObject field = new BasicDBObject();
                    field.put("other", 1);
                    DBCursor cursor1 = collection1.find(query, field);

                    BasicDBObject obj = (BasicDBObject) cursor1.next();
                    count_other = obj.getInt("other");

                    System.out.println(count_other);

                    BasicDBObject searchUpdate = new BasicDBObject();
                    searchUpdate.append("other", count_other);

                    BasicDBObject UpdateObject = new BasicDBObject();
                    UpdateObject.append("$set", new BasicDBObject("other", count_other - 1));

                    collection1.update(searchUpdate, UpdateObject);

                    System.out.println("Checking...3");

                    time_diff = exit_time_mills - in_time_mills;
                    time_diff = time_diff / 1000;
                    time_diff = time_diff / 60;
                    System.out.println("difftime" + time_diff);
                    if (time_diff > 60)
                        charges = 60 + (time_diff);
                    else
                        charges = 60;

                    //sessiuon update....

                    int income_other;
                    BasicDBObject query2 = new BasicDBObject();
                    BasicDBObject field2 = new BasicDBObject();
                    field2.put("other_income", 1);
                    DBCursor cursor2 = collection2.find(query2, field2);

                    BasicDBObject obj2 = (BasicDBObject) cursor2.next();
                    income_other = obj2.getInt("other_income");
                    System.out.println(income_other);

                    BasicDBObject searchUpdate2 = new BasicDBObject();
                    searchUpdate2.append("other_income", income_other);

                    BasicDBObject UpdateObject2 = new BasicDBObject();
                    UpdateObject2.append("$set", new BasicDBObject("other_income", income_other + charges));

                    collection2.update(searchUpdate2, UpdateObject2);

                    JOptionPane.showMessageDialog(null, "Vehicle Out !\n" +
                    /*"In Time : " + in_time_mills + "\n" +*/
                            "Extra Parking Time : " + time_diff + "Min.\n" + "Charges : " + charges);

                    System.out.println("Checked...3");
                    setVisible(false);
                    new Entry_OR_Exit().setVisible(true);
                    break;

                }
            }
            //}
        }
        if (flag == 0) {
            JOptionPane.showMessageDialog(null, "Invalid Vehicle Number or Type");
            setVisible(false);
            new Entry_OR_Exit().setVisible(true);
        }
        /*
        BasicDBObject searchUpdate = new BasicDBObject();
        searchUpdate.append("wheeler_2", count_bike);
                
        BasicDBObject UpdateObject = new BasicDBObject();
        UpdateObject.append("$set", new BasicDBObject("wheeler_2", count_bike + 1));
                
        collection.update(searchUpdate, UpdateObject);
                
        */
        //}
    } catch (Exception e) {

    }

}

From source file:gMIRC_handler.java

/**
 * Delete a user from a channel (used in leave method)
 *
 * @param Channel/*from  ww w .  j  av a2s  . com*/
 * @param Username
 * @return code
 */
private int DeleteChannelUser(String Channel, String Username) {
    int ret = 0;
    try {

        MongoClient mongoClient = new MongoClient();
        DB db = mongoClient.getDB("mirc");
        DBCollection coll = db.getCollection("channelCollection");
        BasicDBObject query = new BasicDBObject("username", Username).append("channel", Channel);

        DBCursor cursor = coll.find(query);

        try {
            ret = 1;
            while (cursor.hasNext()) {
                ret = 1;
                coll.remove(cursor.next());
                ret = 0;
            }
        } finally {
            cursor.close();
        }

    } catch (UnknownHostException ex) {
        Logger.getLogger(gMIRC_handler.class.getName()).log(Level.SEVERE, null, ex);
    }
    System.out.println(Username + " has leaved Channel : " + Channel);
    return ret;
}

From source file:gMIRC_handler.java

/**
 * Delete a user in all channel (used in cleaning)
 *
 * @param Username//from w ww.ja  v  a  2  s .co m
 * @return code
 */
public int DeleteUserInChannel(String Username) {
    int ret = 0;
    try {

        MongoClient mongoClient = new MongoClient();
        DB db = mongoClient.getDB("mirc");
        DBCollection coll = db.getCollection("channelCollection");
        BasicDBObject query = new BasicDBObject("username", Username);

        DBCursor cursor = coll.find(query);

        try {
            while (cursor.hasNext()) {
                coll.remove(cursor.next());
            }
        } finally {
            cursor.close();
        }

    } catch (UnknownHostException ex) {
        Logger.getLogger(gMIRC_handler.class.getName()).log(Level.SEVERE, null, ex);
    }
    System.out.println(Username + " has been deleted in Channel Collection!");
    return ret;
}

From source file:gMIRC_handler.java

/**
 * Moved active user to a passive user (soon to be deleted)
 *
 * @param username/*www  . j a  v a2 s  .c  o  m*/
 * @return code
 */
public static int SoftDelete(String username) {
    int ret = 0;
    try {
        MongoClient mongoClient = new MongoClient();
        DB db = mongoClient.getDB("mirc");
        DBCollection coll = db.getCollection("activeUser");
        DBCollection coll2 = db.getCollection("passiveUser");
        BasicDBObject query = new BasicDBObject("username", username);

        DBCursor cursor = coll.find(query);

        try {
            if (cursor.hasNext()) {
                DBObject temp = cursor.next();
                coll2.insert(temp);
                coll.remove(temp);
            } else {
                ret = 1;
            }
        } finally {
            cursor.close();
        }
    } catch (UnknownHostException ex) {
        Logger.getLogger(gMIRC_handler.class.getName()).log(Level.SEVERE, null, ex);
    }
    return ret;
}

From source file:gMIRC_handler.java

public String GetMessage(String username) {
    String ret = "";
    try {//from   w ww  .ja va 2s .c o  m
        MongoClient mongoClient = new MongoClient();
        DB db = mongoClient.getDB("mirc");
        DBCollection coll = db.getCollection("inbox");
        BasicDBObject query = new BasicDBObject("target", username);
        JSONObject obj = new JSONObject();
        JSONArray arr = new JSONArray();
        DBCursor cursor = coll.find(query);

        try {
            while (cursor.hasNext()) {
                BasicDBObject temp = (BasicDBObject) cursor.next();
                JSONObject sav = new JSONObject();
                sav.put("target", temp.getString("target"));
                sav.put("username", temp.getString("username"));
                sav.put("channel", temp.getString("channel"));
                sav.put("message", temp.getString("message"));
                sav.put("timestamp", temp.getLong("timestamp"));
                arr.add(sav);
                coll.remove(temp);
            }
            obj.put("msg", arr);
            ret = obj.toJSONString();
        } finally {
            cursor.close();
        }
    } catch (UnknownHostException ex) {
        Logger.getLogger(gMIRC_handler.class.getName()).log(Level.SEVERE, null, ex);
    }
    UpdateLastActive(username);
    return ret;
}

From source file:admin_change_Emp.java

private void delete_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_delete_buttonActionPerformed
    // TODO add your handling code here:

    try {/*from   w  w w . ja v a 2  s  .  c o  m*/
        userid = userid_field.getText();
        int flag = 0;

        MongoClient mc = new MongoClient("localhost", 27017);
        DB db = mc.getDB("parking_system");
        DBCollection collection = db.getCollection("employee_info");

        BasicDBObject query = new BasicDBObject();
        BasicDBObject field = new BasicDBObject();
        field.put("_id", userid);

        DBCursor cursor = collection.find(query, field);
        while (cursor.hasNext()) {
            DBObject obj = (DBObject) cursor.next();
            userid_db = obj.get("_id").toString();

            if (userid.equals(userid_db)) {

                flag = 1;

                BasicDBObject deleteObject = new BasicDBObject();
                deleteObject.put("_id", userid);
                DBCursor findQuery = collection.find(deleteObject);
                collection.remove(deleteObject);

                JOptionPane.showMessageDialog(null, "Employee Entry Deleted !!!");

                setVisible(false);
                new Admin_rights().setVisible(true);
                break;
            }
        }
        if (flag == 0) {
            JOptionPane.showMessageDialog(null, "UserID invalid");
        }
    } catch (Exception e) {

    }
}

From source file:RequestHandler.java

public void delete(HttpServletRequest request, HttpServletResponse response) {
    DB db = mongo.getDB("test");
    DBCollection dbc = db.getCollection(collection);
    BasicDBObject add = new BasicDBObject();
    try {/*from  ww w  . ja  v  a 2 s.  c o  m*/
        PrintWriter out = response.getWriter();
        out.println(request.getParameter("selected_object_id"));
    } catch (Exception e) {

    }
    System.out.println(request.getParameter("selected_object_id"));
    add.put("_id", request.getParameter("selected_object_id"));
    dbc.remove(add);
    retrieve(request, response);

}

From source file:DeleteRecord.java

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    // TODO add your handling code here:
    MongoClient mongoClient;/*w  w  w . j a  v  a  2  s  . c  o m*/
    try {
        mongoClient = new MongoClient("localhost", 27017);
        DB db = mongoClient.getDB("Classic_Hangman");
        System.out.println("Connected to database successfully!!");
        BasicDBObject doc = new BasicDBObject();
        DBCollection coll = db.getCollection("movies");
        DBCollection coll1 = db.getCollection("counter");
        System.out.println("Collection selected successfully");
        DBObject c = coll1.findOne();
        int count = ((Number) c.get("count")).intValue();
        int del_id = Integer.parseInt(jTextField1.getText());
        doc.put("id", del_id);
        coll.remove(doc);
        for (int i = del_id + 1; i <= count; i++) {
            BasicDBObject newDocument = new BasicDBObject();
            newDocument.append("$set", new BasicDBObject().append("id", i - 1));
            BasicDBObject searchQuery = new BasicDBObject().append("id", i);
            coll.update(searchQuery, newDocument);
        }
        count--;
        coll1.remove(new BasicDBObject());
        BasicDBObject doc1 = new BasicDBObject("count", count);
        coll1.insert(doc1);
        JOptionPane.showMessageDialog(this, "Record deleted!");
    } catch (UnknownHostException ex) {
        Logger.getLogger(AdminLogin.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:AddRecord.java

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    // TODO add your handling code here:
    int flag = 0;
    try {//from w w  w  .  j  a va2  s  .  com
        // TODO add your handling code here:
        MongoClient mongoClient = new MongoClient("localhost", 27017);
        db = mongoClient.getDB("Classic_Hangman");
        System.out.println("Connected to database successfully!!");
        BasicDBObject d = new BasicDBObject();
        String movie = jTextField1.getText();
        //String genre = jTextField2.getText();
        String director = jTextField3.getText();
        for (int i = 0; i < director.length(); i++) {
            if (Character.isDigit(director.charAt(i))) {
                JOptionPane.showMessageDialog(this, "Invalid director entry!");
                flag = 1;
                break;
            }
        }
        if (movie.equals("") || genre.equals("")) {
            JOptionPane.showMessageDialog(this, "Empty Field!");
        } else {
            if (flag != 1) {
                DBCollection coll = db.getCollection("movies");
                DBCollection coll1 = db.getCollection("counter");
                System.out.println("Collection selected successfully");
                BasicDBObject query = new BasicDBObject();
                DBObject c = coll1.findOne();
                count = ((Number) c.get("count")).intValue();
                System.out.println("Count = " + count);
                //count = (int)c.get("count");
                BasicDBObject doc = new BasicDBObject("id", count + 1).append("name", movie)
                        .append("director", director).append("genre", genre);
                coll.insert(doc);
                System.out.println("Document inserted successfully");
                count++;
                /*DBCursor cursor = coll.find();
                int i=1;
                while (cursor.hasNext()) { 
                    System.out.println("Inserted Document: "+i); 
                    System.out.println(cursor.next()); 
                    i++;
                }*/
                //DBCollection coll1 = db.getCollection("counter");
                //System.out.println("Collection selected successfully");
                coll1.remove(new BasicDBObject());
                BasicDBObject doc1 = new BasicDBObject("count", count);
                coll1.insert(doc1);
                /*cursor = coll.find();
                BasicDBObject newDocument = new BasicDBObject();
                System.out.println(count);
                newDocument.put("count", count);
                BasicDBObject searchQuery = new BasicDBObject().append("count", count);
                coll.update(searchQuery, newDocument);*/
                jTextField1.setText("");
                //jTextField2.setText("");
                jTextField3.setText("");
                JOptionPane.showMessageDialog(this, "Record added!");
            }
        }
    } catch (UnknownHostException ex) {
        //Logger.getLogger(AdminLogin.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:PersistMongo.java

public static void main(String[] args) {
    // conectando ao mongo, como esta sem parametros
    //se conecta ao mongo local por default 
    MongoClient mongoClient = new MongoClient("localhost", 27017);

    // Base de dados
    DB db = (DB) mongoClient.getDB("JavaDB");
    // Tabela //from   w  ww .j  a va  2 s .c o  m
    DBCollection collection = db.getCollection("colJava");
    // objeto para ser persistido
    BasicDBObject document1 = new BasicDBObject();
    document1.put("id", 1001);
    document1.put("msg", "Persistencia com mongoDB");

    // salvando o documento na base nosql
    collection.insert(document1);
    BasicDBObject document = new BasicDBObject();
    document = new BasicDBObject();
    document.put("id", 1002);
    document.put("msg", "Java + Mongo");

    // salvando o documento na base nosql
    collection.insert(document);
    // objeto que recebe os dados do banco
    DBCursor cursor = collection.find();
    // percorrendo o objeto e mostrando os dados
    while (cursor.hasNext()) {
        System.out.println("Document: " + cursor.next());

    }
    // Passando o documento que sera removido
    collection.remove(document);
    // Passando o documentoque sera atualizado e o documento que entrara em seu lugar 
    collection.update(document, document1);

}