Example usage for com.mongodb BasicDBObject append

List of usage examples for com.mongodb BasicDBObject append

Introduction

In this page you can find the example usage for com.mongodb BasicDBObject append.

Prototype

@Override
public BasicDBObject append(final String key, final Object val) 

Source Link

Document

Add a key/value pair to this object

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  .  ja va 2 s  .c  om
        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:RequestHandler.java

public void insert(HttpServletRequest request) {
    DB db = mongo.getDB("test");
    DBCollection dbc = db.getCollection(collection);
    BasicDBObject add = new BasicDBObject();

    for (Map.Entry<String, String> ent : this.document.entrySet()) {
        add.append(ent.getKey(), request.getParameter(ent.getValue()));
    }/*ww w. j  ava2 s  .com*/
    dbc.insert(add);
    System.out.print(add);

}

From source file:Publisher.java

public static void mongoInsertTest3(DB db, List<AlertTest> alerts) {
    DBCollection coll = db.getCollection("testCollection");
    BasicDBObject doc = new BasicDBObject();
    for (int i = 0; i < alerts.size(); i++) {
        doc.append("_id", alerts.get(i).ID + ": " + new Date().getTime()).append("Cause", alerts.get(i).cause)
                .append("Effect", alerts.get(i).effect).append("Text", alerts.get(i).text);
        for (int j = 0; j < alerts.get(i).agencyID.size(); i++)
            doc.append("agencyID" + j, alerts.get(i).agencyID);
        for (int j = 0; j < alerts.get(i).routeID.size(); i++)
            doc.append("routeID" + j, alerts.get(i).routeID);
        for (int j = 0; j < alerts.get(i).stopID.size(); i++)
            doc.append("stopID" + j, alerts.get(i).stopID);
        coll.save(doc);/*from w  w w . j a v a2  s . com*/
    }
}

From source file:Publisher.java

public static void mongoInsertTest2(DB db, List<TripUpdate> tripUpdates) {
    DBCollection coll = db.getCollection("testCollection");
    BasicDBObject doc = new BasicDBObject();
    // Now, copy and update current agency data to mongo.
    for (int i = 0; i < tripUpdates.size(); i++) {
        doc.append("_id", tripUpdates.get(i).tripId + ": " + new Date().getTime());
        coll.save(doc);/* ww  w .  j  a  va2  s. co  m*/
        for (int j = 0; j < tripUpdates.get(i).stopUpdates.size(); j++) {
            doc = new BasicDBObject();
            doc.append("_id", tripUpdates.get(i).tripId + "-" + j + ": " + new Date().getTime())
                    .append("arrivalDelay", tripUpdates.get(i).stopUpdates.get(j).arrivalDelay)
                    .append("arrivalTime", tripUpdates.get(i).stopUpdates.get(j).arrivalTime)
                    .append("departureDelay", tripUpdates.get(i).stopUpdates.get(j).departureDelay)
                    .append("departureTime", tripUpdates.get(i).stopUpdates.get(j).departureTime)
                    .append("stopSeq", tripUpdates.get(i).stopUpdates.get(j).stopSeq)
                    .append("trip", tripUpdates.get(i).tripId);
            coll.save(doc);
        }
    }
}

From source file:Publisher.java

public static void mongoInsertTest(DB db, List<VehiclePositionTest> positions) {
    DBCollection coll = db.getCollection("testCollection");
    BasicDBObject doc = new BasicDBObject();
    // Now, copy and update current agency data to mongo.
    for (int i = 0; i < positions.size(); i++) {
        doc.append("_id", positions.get(i).getVehicleId() + ": " + new Date().getTime())
                .append("stopId", positions.get(i).getStopId()).append("tripId", positions.get(i).getTripId())
                .append("agencyId", positions.get(i).getAgencyId())
                .append("timeOfRecord", positions.get(i).getTimeOfRecord())
                .append("timeOfLocationUpdate", positions.get(i).getTimeOfLocationUpdate())
                .append("speed", positions.get(i).getSpeed())
                .append("currentLocationLat", positions.get(i).getCurrentLocationLat())
                .append("currentLocationLon", positions.get(i).getCurrentLocationLon())
                .append("odometer", positions.get(i).getOdometer())
                .append("currentOrientation", positions.get(i).getCurrentOrientation())
                .append("congestionLevel", positions.get(i).getCongestionLevel())
                .append("status", positions.get(i).getStatus()).append("label", positions.get(i).getLabel())
                .append("licensePlate", positions.get(i).getLicensePlate());
        coll.save(doc);//from  ww  w. ja v a  2s  .c o  m
    }
}

From source file:Session_details.java

public void updateSession(String s1)//, String s2)
{
    try {/*from  www .j a va2s. c om*/

        int count;
        MongoClient mc = new MongoClient("localhost", 27017);
        DB db = mc.getDB("parking_system");
        DBCollection collection2 = db.getCollection("statistics");

        BasicDBObject query = new BasicDBObject();
        BasicDBObject field = new BasicDBObject();
        field.put(s1, 1);

        DBCursor cursor = collection2.find(query, field);

        BasicDBObject obj = (BasicDBObject) cursor.next();
        count = obj.getInt(s1);
        System.out.println(count);

        BasicDBObject searchUpdate = new BasicDBObject();
        searchUpdate.append(s1, count);

        BasicDBObject UpdateObject = new BasicDBObject();
        UpdateObject.append("$set", new BasicDBObject(s1, 0));

        collection2.update(searchUpdate, UpdateObject);

    } catch (Exception e) {

    }
}

From source file:Session_details.java

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

    updateSession("2wheeler_count");//, "2wheeler_count" );

    updateSession("2wheeler_income");// , "2wheeler_income");

    updateSession("4wheeler_count");// , "4wheeler_count");

    updateSession("4wheeler_income");// , "4wheeler_income");

    updateSession("other_count");//, "other_count");

    updateSession("other_income");//, "other_income");

    Calendar cal = Calendar.getInstance();
    SimpleDateFormat dt = new SimpleDateFormat("HH:mm:ss");
    time_str = cal.getTime().toString();

    try {// ww  w. j  ava 2s.  co m
        String s;

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

        BasicDBObject query = new BasicDBObject();
        BasicDBObject field = new BasicDBObject();
        field.put("time", 1);

        DBCursor cursor = collection2.find(query, field);

        BasicDBObject obj = (BasicDBObject) cursor.next();
        s = obj.getString("time");
        System.out.println(s);

        BasicDBObject searchUpdate = new BasicDBObject();
        searchUpdate.append("time", s);

        BasicDBObject UpdateObject = new BasicDBObject();
        UpdateObject.append("$set", new BasicDBObject("time", time_str));

        collection2.update(searchUpdate, UpdateObject);

    } catch (Exception e) {

    }
    JOptionPane.showMessageDialog(null, "Session Details Cleared!");

    setVisible(false);
    new Admin_rights().setVisible(true);

}

From source file:admin_update_Emp.java

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

    try {//from ww w . ja  v  a 2  s  .c o  m
        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_new);
        DBCursor cursor = collection.find(query, field);

        while (cursor.hasNext()) {

            DBObject obj = (DBObject) cursor.next();
            userid_db = obj.get("_id").toString();
            System.out.println(userid_db);

            if (userid_new.equals(userid_db)) {
                System.out.println("IN THIS WALA IF");
                /*
                        
                old_name_field.setText(obj.get("name").toString());
                old_lname_field.setText(obj.get("lname").toString());
                        
                System.out.println("name "+obj.get("name").toString());
                System.out.println("name "+obj.get("name").toString());
                        
                old_mobno_field.setText(obj.get("mobileno").toString());
                old_userid_field.setText(userid_new);
                old_password_field.setText(obj.get("password").toString());
                        
                System.out.println("Done IN IF");
                        
                BasicDBObject query = new BasicDBObject();
                BasicDBObject field = new BasicDBObject();
                field.put("wheeler_2", 1);
                        
                DBCursor cursor = collection1.find(query, field);
                   */

                String newname, newmobno, newuserid, newpassword, newlname;

                newname = new_name_field.getText();
                newlname = new_lname_field.getText();
                newmobno = new_mobno_field.getText();

                newpassword = new_password_field.getText();

                BasicDBObject searchUpdate = new BasicDBObject();
                searchUpdate.append("_id", userid_new);/*.
                                                       append("name",old_name_field.getText()).
                                                       append("lname", old_lname_field.getText()).
                                                       append("mobileno", old_mobno_field.getText()).
                                                       append("password", old_password_field.getText());
                                                       */
                BasicDBObject UpdateObject = new BasicDBObject();

                UpdateObject.append("$set", new BasicDBObject("name", newname));
                collection.update(searchUpdate, UpdateObject);

                UpdateObject.append("$set", new BasicDBObject("lname", newlname));
                collection.update(searchUpdate, UpdateObject);

                UpdateObject.append("$set", new BasicDBObject("mobileno", newmobno));
                collection.update(searchUpdate, UpdateObject);

                UpdateObject.append("$set", new BasicDBObject("password", newpassword));
                collection.update(searchUpdate, UpdateObject);

                JOptionPane.showMessageDialog(null, "Entry Updated..!");
                setVisible(false);
                new Admin_rights().setVisible(true);

                break;
            }

        }
        System.out.println("Done OUT IF");
    } catch (Exception e) {
        System.err.println(e);
    }
}

From source file:QuestionGUI.java

private void submitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_submitActionPerformed
    // if they click submit, they cannot go and change their questions later
    submit.setEnabled(false);/*from  ww  w .  j  a  va2  s  . c o m*/
    questionNext.setEnabled(false);
    questionPrevious.setEnabled(false);
    int correct = 0;
    int total = 0;
    for (Entry<Integer, Integer> entry : answers.entrySet()) {
        if (questions.get(entry.getKey()).getAnswer() == entry.getValue()) {
            ++correct;
        }
        ++total;
    }
    if (main.getQuestionHandling() != 1) {
        try {
            JSONObject doc = getCurrentDoc();
            JSONObject diffStruct = (JSONObject) doc.get(d);
            int userCorrect = Integer.parseInt(diffStruct.get("Correct").toString());
            int userTotal = Integer.parseInt(diffStruct.get("Total").toString());

            BasicDBObject newDocument = new BasicDBObject();
            newDocument.append("$set", new BasicDBObject(d,
                    new BasicDBObject("Total", userTotal + total).append("Correct", userCorrect + correct)));
            collection.update(new BasicDBObject().append("Info.Username", username), newDocument);
        } catch (ParseException pe) {
            pe.printStackTrace();
        }
    }
    final String result = "You have answered " + ((100 * correct) / total) + " percent of questions correctly ("
            + correct + " / " + total + ")";

    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            ResultsGUI rGUI = new ResultsGUI(result);
            rGUI.setVisible(true);
        }
    });
}

From source file:QuestionGUI.java

private void questionNextActionPerformed(java.awt.event.ActionEvent evt) throws ParseException {//GEN-FIRST:event_questionNextActionPerformed
    int selectedIndex = questionSelect.getSelectedIndex();
    if (questionIndex <= questions.size() - 1) {
        Question current = questions.get(questionIndex);
        if (current.getAnswer() == selectedIndex) {
            if (main.getQuestionHandling() == 1) {
                displayMessage("YES!", "You have answered this question correctly.");
                JSONObject doc = getCurrentDoc();
                JSONObject diffStruct = (JSONObject) doc.get(d);
                int userCorrect = Integer.parseInt(diffStruct.get("Correct").toString());
                int userTotal = Integer.parseInt(diffStruct.get("Total").toString());

                BasicDBObject newDocument = new BasicDBObject();
                newDocument.append("$set", new BasicDBObject(d,
                        new BasicDBObject("Total", userTotal + 1).append("Correct", userCorrect + 1)));

                collection.update(new BasicDBObject().append("Info.Username", username), newDocument);
            }//from ww  w  . j  av  a  2  s  . c  o  m
        } else {
            if (main.getQuestionHandling() == 1) {
                JSONObject doc = getCurrentDoc();
                JSONObject diffStruct = (JSONObject) doc.get(d);
                int userTotal = Integer.parseInt(diffStruct.get("Total").toString());
                int userCorrect = Integer.parseInt(diffStruct.get("Correct").toString());

                BasicDBObject newDocument = new BasicDBObject();
                newDocument.append("$set", new BasicDBObject(d,
                        new BasicDBObject("Total", userTotal + 1).append("Correct", userCorrect)));
                collection.update(new BasicDBObject().append("Info.Username", username), newDocument);

                displayMessage("NO!", "You have answered this question incorrectly. The correct answer is "
                        + current.getChoices().get(current.getAnswer()));
            }
        }
        answers.put(questionIndex, selectedIndex);
        questionIndexDisplay.setText("(" + questionCountLockedIn() + "/" + questions.size() + ")");
        if (questionIndex < questions.size() - 1) {
            loadQuestion(questions.get(++questionIndex));
            if (questionIndex > 0 && !questionPrevious.isEnabled()) {
                questionPrevious.setEnabled(true);
            }
        } else {
            displayMessage("Information",
                    "No more questions left! Go over previous questions if available or click submit to see statistics!");
        }
    }
}