Example usage for com.mongodb BasicDBObject BasicDBObject

List of usage examples for com.mongodb BasicDBObject BasicDBObject

Introduction

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

Prototype

public BasicDBObject() 

Source Link

Document

Creates an empty object.

Usage

From source file:Parent.java

private void formComponentResized(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentResized
    // TODO add your handling code here:

    DBCollection coll = db.getCollection("parent");
    BasicDBObject andQuery = new BasicDBObject();
    List<BasicDBObject> obj = new ArrayList<BasicDBObject>();
    obj.add(new BasicDBObject("firstname", firstname));
    obj.add(new BasicDBObject("lastname", lastname));

    andQuery.put("$and", obj);
    DBCursor cursor = coll.find(andQuery);
    while (cursor.hasNext()) {
        BasicDBList sons = (BasicDBList) cursor.next().get("sons");
        String data[] = new String[sons.size()];
        for (int j = 0; j < sons.size(); ++j) {
            BasicDBObject son = (BasicDBObject) sons.get(j);
            data[j] = son.get("name").toString();

        }/*from  w  w w  .j  a  va 2  s .c  om*/

        SonList.setListData(data);

    }
}

From source file:Parent.java

private void SonListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_SonListValueChanged
    // TODO add your handling code here:
    if (!evt.getValueIsAdjusting()) {
        DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
        model.setRowCount(0);// ww  w. j  a va  2s .c o m

        String selected = SonList.getSelectedValue();
        DBCollection coll = db.getCollection("student");
        BasicDBObject andQuery = new BasicDBObject();
        List<BasicDBObject> obj = new ArrayList<BasicDBObject>();
        obj.add(new BasicDBObject("firstname", selected));
        obj.add(new BasicDBObject("mid_name", firstname));
        obj.add(new BasicDBObject("lastname", lastname));
        andQuery.put("$and", obj);
        DBCursor cursor = coll.find(andQuery);
        while (cursor.hasNext()) {

            BasicDBList marks = (BasicDBList) cursor.next().get("marks");
            for (int j = 0; j < marks.size(); ++j) {
                BasicDBObject mark = (BasicDBObject) marks.get(j);
                String material = mark.get("course").toString();
                String teacher = mark.get("teacher").toString();
                if (mark.get("mid") == null) {

                    Object[] row = { material, teacher, "not readay", "not ready" };
                    model.addRow(row);

                } else if (mark.get("mid") != null && mark.get("final") == null) {

                    Object[] row = { material, teacher, mark.get("mid"), "not ready" };
                    model.addRow(row);
                } else {

                    Object[] row = { material, teacher, mark.get("mid"), mark.get("final") };
                    model.addRow(row);

                }
            }
        }
    }

}

From source file:admin_change_Emp.java

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

    try {/*from   w w w  .j  ava  2  s  .  c  om*/
        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;
                setVisible(false);
                new admin_update_Emp(userid).setVisible(true);
                break;
            }
        }
        if (flag == 0) {
            JOptionPane.showMessageDialog(null, "UserID invalid");
        }

    } catch (Exception e) {

    }

}

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 {// www . j  a  v  a  2s .c  om
        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:UserHistoryGUI.java

/**
 * @param args the command line arguments
 * @throws ParseException /*from  ww w  .  j  a  v  a  2 s  . c om*/
 */

private void initScores() throws ParseException {
    DBCollection collection = db.getCollection("Users");

    DBObject query = new BasicDBObject();
    query.put("Info.Username", username);
    DBObject fields = new BasicDBObject();
    fields.put("Beginner", 1);
    fields.put("Easy", 1);
    fields.put("Medium", 1);
    fields.put("Hard", 1);
    fields.put("Last", 1);

    DBCursor scoreCursor = collection.find(query, fields);
    JSONParser parser = new JSONParser();
    int totalScore = 0;
    int totalAnswered = 0;
    int totalCorrect = 0;

    while (scoreCursor.hasNext()) {
        String json = scoreCursor.next().toString();
        JSONObject jsonFile = (JSONObject) parser.parse(json);

        //beginner scores
        JSONObject beginner = (JSONObject) jsonFile.get("Beginner");
        int beginnerTotal = Integer.parseInt(beginner.get("Total").toString());
        totalAnswered += beginnerTotal;
        int beginnerCorrect = Integer.parseInt(beginner.get("Correct").toString());
        totalCorrect += beginnerCorrect;
        totalScore += beginnerCorrect * 10;
        float beginnerRate = 0.0f;
        if (beginnerTotal != 0) {
            beginnerRate = (float) beginnerCorrect / beginnerTotal;
        }

        //easy scores
        JSONObject easy = (JSONObject) jsonFile.get("Easy");
        int easyTotal = Integer.parseInt(easy.get("Total").toString());
        int easyCorrect = Integer.parseInt(easy.get("Correct").toString());
        totalAnswered += easyTotal;
        totalCorrect += easyCorrect;
        totalScore += easyCorrect * 20;

        float easyRate = 0.0f;
        if (easyTotal != 0) {
            easyRate = (float) easyCorrect / easyTotal;
        }

        //med scores
        JSONObject med = (JSONObject) jsonFile.get("Medium");
        int medTotal = Integer.parseInt(med.get("Total").toString());
        int medCorrect = Integer.parseInt(med.get("Correct").toString());

        totalAnswered += medTotal;
        totalCorrect += medCorrect;
        totalScore += medCorrect * 30;

        float medRate = 0.0f;
        if (medTotal != 0) {
            medRate = (float) medCorrect / medTotal;
        }

        // hard scores
        JSONObject hard = (JSONObject) jsonFile.get("Hard");
        int hardTotal = Integer.parseInt(hard.get("Total").toString());
        int hardCorrect = Integer.parseInt(hard.get("Correct").toString());

        totalAnswered += hardTotal;
        totalCorrect += hardCorrect;
        totalScore += hardCorrect * 50;

        float hardRate = 0.0f;
        if (hardTotal != 0) {
            hardRate = (float) hardCorrect / hardTotal;
        }

        // Question
        JSONObject q = (JSONObject) jsonFile.get("Last");
        String question = q.get("Question").toString();
        String answer = q.get("Answer").toString();
        String correct = q.get("Correct").toString();

        BeginnerTotal.setText("" + beginnerTotal);
        BeginnerCorrect.setText("" + beginnerCorrect);
        BeginnerRate.setText("" + beginnerRate);

        EasyTotal.setText("" + easyTotal);
        EasyCorrect.setText("" + easyCorrect);
        EasyRate.setText("" + easyRate);

        MedTotal.setText("" + medTotal);
        MedCorrect.setText("" + medCorrect);
        MedRate.setText("" + medRate);

        HardTotal.setText("" + hardTotal);
        HardCorrect.setText("" + hardCorrect);
        HardRate.setText("" + hardRate);

        TotalScore.setText("" + totalScore);
        jTextArea2.setText(question);
        Answer.setText(answer);
        CorrectorNo.setText(correct);
    }

}

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()));
    }//from   ww w  .java  2  s.  c o m
    dbc.insert(add);
    System.out.print(add);

}

From source file:RequestHandler.java

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

    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><script>" + "function myFunction(elt) {"
                + " elt.setAttribute('selected_object_id', elt.getAttribute('id'));"
                + "var xhr = new XMLHttpRequest();"
                + "    xhr.open('GET', 'http://localhost:8080/MongoCraig/DeleteInfo', true);"
                + "    xhr.send(null);}" + "</script></head>");
        DBCursor cursor = dbc.find();/*  ww  w . j ava2 s .c  o  m*/
        out.println("<body>");
        for (DBObject record : cursor) {
            out.println("<p>First Name : " + record.get("firstname") + "</p>");
            out.println("<p>Last Name : " + record.get("lastname") + "</p>");
            out.println("<p>What : " + record.get("what") + "</p>");
            out.println("<p>Price : " + record.get("price") + "</p>");
            out.println("<button id=" + record.get("_id") + " onclick='myFunction(this); '>DELETE</button> ");

        }
        out.println("</body>");
        out.println("</html>");
    } 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 {/* w  ww  .ja  v  a2 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:UnitTest3.java

License:Open Source License

public static int testgridfs() {

    long time1;//from  w  ww.j  ava2s .  c  o  m
    long time2;
    long time3;
    long time4;

    try {

        MongoClient mongo = new MongoClient("localhost", 27017);
        DB db = mongo.getDB("JFileDB");

        // TODO JFileMetaDataTable should be in MetaDB database
        DBCollection collection = db.getCollection("JFileMetaDataTable");

        String newFileName = "com.dilmus.scabi.testdata.in.App.class";

        File jFile = new File("/home/anees/workspace/testdata/in/App.class");

        // create a JFileTable namespace
        GridFS gfsj = new GridFS(db, "JFileTable");

        // get file from local drive
        GridFSInputFile gfsFile = gfsj.createFile(jFile);

        // set a new filename for identify purpose
        gfsFile.setFilename(newFileName);
        gfsFile.setContentType("class"); // jar, zip, war
        // save the image file into mongoDB
        gfsFile.save();

        // Let's create a new JSON document with some "metadata" information
        BasicDBObject info = new BasicDBObject();
        info.put("DBHost", "localhost");
        info.put("DBPort", "27017");
        info.put("JFileName", newFileName);
        info.put("JFileID", gfsFile.getId());
        info.put("JFileMD5", gfsFile.getMD5());
        collection.insert(info, WriteConcern.ACKNOWLEDGED);

        // print the result
        DBCursor cursor = gfsj.getFileList();
        while (cursor.hasNext()) {
            System.out.println(cursor.next());
        }

        DBCursor cursor2 = collection.find();

        while (cursor2.hasNext()) {
            System.out.println(cursor2.next());
        }

        // get file by it's filename
        GridFSDBFile jForOutput = gfsj.findOne(newFileName);

        // save it into a new image file
        jForOutput.writeTo("/home/anees/workspace/testdata/out/AppOut.class");

        // remove the file from mongoDB
        // gfsj.remove(gfsj.findOne(newFileName));

        System.out.println("Done");
        mongo.close();
    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (MongoException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return 0;
}

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 www. j a  v  a  2 s .co  m
    }
}