Example usage for com.mongodb DBObject get

List of usage examples for com.mongodb DBObject get

Introduction

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

Prototype

Object get(String key);

Source Link

Document

Gets a field from this object by a given name.

Usage

From source file:MashUp.java

public String queryDatabaseMashUp(String Year, String UsedApi, String Tags, String Keywords) {
    BasicDBObject gtQuery = new BasicDBObject();

    if (!Year.equals("")) {
        //gtQuery.put("rating", new BasicDBObject("$gt", "4"));
        gtQuery.put("updated", new BasicDBObject("$regex", ".*" + Year + ".*"));
    }//  ww w .  ja v a  2s . c o m
    if (!UsedApi.equals("")) {
        System.out.println("UsedApi " + UsedApi);
        // DBObject clause1 = new BasicDBObject("APIS", new BasicDBObject("$regex",".*"+UsedApi+".*"));  
        // gtQuery.put("APIs",new BasicDBObject("APIs", new BasicDBObject("$regex",".*"+UsedApi+".*")));
        gtQuery.put("APIs", new BasicDBObject("$regex", ".*" + UsedApi + ".*"));
    }

    if (!Tags.equals("")) {
        gtQuery.put("Tags", new BasicDBObject("$regex", ".*" + Tags + ".*"));
    }
    if (!Keywords.equals("")) {
        DBObject clause1 = new BasicDBObject("summary", new BasicDBObject("$regex", ".*" + Keywords + ".*"));
        DBObject clause2 = new BasicDBObject("description",
                new BasicDBObject("$regex", ".*" + Keywords + ".*"));
        DBObject clause3 = new BasicDBObject("title", new BasicDBObject("$regex", ".*" + Keywords + ".*"));
        BasicDBList or = new BasicDBList();
        or.add(clause1);
        or.add(clause3);
        or.add(clause2);
        gtQuery.put("$or", or);
        //DBObject query = new BasicDBObject("$or", or);
        //System.out.println(query);

    }

    //gtQuery.put("rating", new BasicDBObject("$gt", "4").append("$lt", "5"));
    //  gtQuery.put("updated", "2012-12-17T09:51:40Z");
    DBCursor cursor = table2.find(gtQuery);
    String temp = "";
    while (cursor.hasNext()) {
        DBObject temp1 = cursor.next();
        temp = temp + " " + temp1.get("name") + "\n";
    }
    return temp;
}

From source file:TeacherSection.java

private void GradeListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_GradeListValueChanged
    // TODO add your handling code here:
    if (!evt.getValueIsAdjusting()) {
        String selected = GradeList.getSelectedValue();
        int grade = 0;
        int section = 0;
        for (int i = 0; i < selected.length(); ++i) {
            if (selected.charAt(i) == '-') {

                grade = (int) Double.parseDouble(selected.substring(0, i));
                section = (int) Double.parseDouble(selected.substring(i + 1));
            }/*from  ww  w  . ja  v  a  2 s . co m*/
        }

        DBCollection coll = db.getCollection("student");
        BasicDBObject andQuery = new BasicDBObject();
        List<BasicDBObject> obj = new ArrayList<BasicDBObject>();

        obj.add(new BasicDBObject("grade", grade));
        obj.add(new BasicDBObject("section", section));

        andQuery.put("$and", obj);
        DBCursor cursor = coll.find(andQuery);
        while (cursor.hasNext()) {

            DBObject object = cursor.next();
            this.FirstnameField.setText(object.get("firstname").toString());
            this.MidnameField.setText(object.get("mid_name").toString());
            LastnameField.setText(object.get("lastname").toString());

            BasicDBList marks = (BasicDBList) object.get("marks");
            for (int i = 0; i < marks.size(); ++i) {
                BasicDBObject mark = (BasicDBObject) marks.get(i);
                if (mark.getString("teacher").equals(firstname)) {

                    if (mark.get("mid") == null) {

                        MidField.setText("-1");
                        FinalField.setText("-1");

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

                        MidField.setText(mark.getString("mid"));
                        FinalField.setText("-1");
                    } else {

                        MidField.setText(mark.getString("mid"));
                        FinalField.setText(mark.getString("final"));

                    }

                    break;
                }

            }
            int reply = JOptionPane.showConfirmDialog(null, "if you want to update press yes", "Update or Skip",
                    JOptionPane.YES_NO_OPTION);
            if (reply == JOptionPane.YES_OPTION) {
                int midmark = Integer.parseInt(JOptionPane.showInputDialog("Please input mark for mid: "));
                int finalmark = Integer.parseInt(JOptionPane.showInputDialog("Please input mark for final: "));
                BasicDBObject andQuery1 = new BasicDBObject();
                List<BasicDBObject> obj1 = new ArrayList<BasicDBObject>();
                obj1.add(new BasicDBObject("firstname", FirstnameField.getText()));
                obj1.add(new BasicDBObject("mid_name", MidnameField.getText()));
                obj1.add(new BasicDBObject("lastname", LastnameField.getText()));
                obj1.add(new BasicDBObject("grade", grade));
                obj1.add(new BasicDBObject("section", section));
                obj1.add(new BasicDBObject("marks.teacher", firstname));
                andQuery1.put("$and", obj1);
                BasicDBObject data = new BasicDBObject();
                if (midmark == -1 && finalmark == -1) {
                    JOptionPane.showMessageDialog(null, "No data was updated!");
                } else if (midmark != -1 && finalmark != -1) {
                    data.put("marks.$.mid", midmark);
                    JOptionPane.showMessageDialog(null, "data was updated .");
                } else {
                    data.put("marks.$.mid", midmark);
                    data.put("marks.$.final", finalmark);
                    JOptionPane.showMessageDialog(null, "data was updated .");

                }
                BasicDBObject command = new BasicDBObject();
                command.put("$set", data);
                coll.update(andQuery1, command);

            }

        }

    }

}

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 {/* www . 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;
                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 {/*w  ww .j  ava  2s .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 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();// w ww .  j  a  va2s .  co  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:fList.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./*ww w.  j ava 2  s.com*/
 *
 * @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 {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter pw = response.getWriter();
    response.setContentType("text/html");

    String username = request.getParameter("username");

    try {
        MongoClient mongo = new MongoClient("localhost", 27017);
        DB db = mongo.getDB("test");
        System.out.println("Connection established");
        DBCollection User = db.getCollection("User");

        DBCursor cursor = User.find();

        while (cursor.hasNext()) {
            DBObject result = cursor.next();
            String s = result.get("username").toString();
            if (!s.equals(username)) {
                response.getWriter().write(s);
                response.getWriter().write(" ");
            }

        }

    } catch (Exception e) {
        System.err.println(e.getClass().getName() + ": " + e.getMessage());
    }
}

From source file:tweetList.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//from w  ww .  j a v a2s  .  com
 *
 * @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 {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    response.setContentType("text/html");
    String username = request.getParameter("username");

    try {
        MongoClient mongo = new MongoClient("localhost", 27017);
        DB db = mongo.getDB("test");
        System.out.println("Connection established");
        DBCollection User = db.getCollection("User");
        DBCollection Tweet = db.getCollection("Tweet");

        BasicDBObject query = new BasicDBObject("username", username);
        DBCursor curssc = User.find(query);
        // out.println(curssc.size());
        DBObject result = curssc.next();
        ArrayList<String> arr2 = new ArrayList();
        arr2 = (ArrayList<String>) result.get("tweet_id");

        ArrayList<String> e = new ArrayList();
        e = (ArrayList<String>) result.get("following_username");
        ArrayList<String> texts = new ArrayList();
        //out.println(e.size());
        //out.println(e.get(0));

        if (e.size() > 0 || arr2.size() > 0) {
            for (int i = e.size() - 1; i >= 0; i--) {
                BasicDBObject query2 = new BasicDBObject("username", e.get(i));
                DBCursor curssc2 = User.find(query2);
                if (curssc2.size() == 1) {
                    DBObject res = curssc2.next();
                    ArrayList<String> arr = new ArrayList();
                    arr = (ArrayList<String>) res.get("tweet_id");

                    texts.addAll(arr);

                }

            }
            texts.addAll(arr2);
            Collections.sort(texts);
            for (int j = texts.size() - 1; j >= 0; j--) {
                BasicDBObject query3 = new BasicDBObject("tweet_id", texts.get(j));
                DBCursor curssc3 = Tweet.find(query3);
                DBObject result1 = curssc3.next();
                response.getWriter().write(result1.get("username").toString());
                response.getWriter().write(":  ");
                response.getWriter().write(result1.get("tweet_text").toString());
                response.getWriter().write("\n");
            }

        } else {
            response.getWriter().write("No tweet");
        }

    } catch (Exception e) {
        System.err.println(e.getClass().getName() + ": " + e.getMessage());
    }
}

From source file:filterFriends.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.// w  ww.j a  va  2 s .  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 {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter pw = response.getWriter();
    response.setContentType("text/html");

    String username = request.getParameter("username");
    String filtered_name = request.getParameter("filtered");

    try {
        MongoClient mongo = new MongoClient("localhost", 27017);
        DB db = mongo.getDB("test");
        System.out.println("Connection established");
        DBCollection User = db.getCollection("User");

        DBCursor cursor = User.find();

        while (cursor.hasNext()) {
            DBObject result = cursor.next();
            String s = result.get("username").toString();
            if (s.equals(filtered_name)) {
                response.getWriter().write(s);
                response.getWriter().write(" ");
            }

        }

    } catch (Exception e) {
        System.err.println(e.getClass().getName() + ": " + e.getMessage());
    }
}

From source file:Publisher.java

public static void mongoQueryTest2(DB db, List<TripUpdate> tripUpdates) {
    DBCollection coll = db.getCollection("testCollection");
    DBObject d = coll.findOne(new BasicDBObject("_id", tripUpdates.get(0).tripId));
    DBObject a = coll.findOne(new BasicDBObject("_id", tripUpdates.get(0).tripId + "-0"));
    System.out.println("********* MONGO OUTPUT *********");
    System.out.println("Trip: " + d.get("_id").toString());
    System.out.println("Stop delay: " + a.get("arrivalDelay").toString());
    System.out.println("**********************************\n");
}

From source file:Publisher.java

public static void mongoQueryTest(DB db, List<VehiclePositionTest> positions) {
    DBCollection coll = db.getCollection("testCollection");
    DBObject d = coll.findOne(new BasicDBObject("_id", positions.get(0).getVehicleId()));
    System.out.println("********* MONGO OUTPUT *********");
    System.out.println("Vehicle: " + d.get("_id").toString());
    System.out.println("Stop: " + d.get("stopId").toString());
    System.out.println("Trip: " + d.get("tripId").toString());
    System.out.println("Agency: " + d.get("agencyId").toString());
    System.out.println("Time of record: " + d.get("timeOfRecord").toString());
    System.out.println("Time of update: " + d.get("timeOfLocationUpdate").toString());
    System.out.println("Speed: " + d.get("speed").toString());
    System.out.println("Latitude: " + d.get("currentLocationLat").toString());
    System.out.println("Longitude: " + d.get("currentLocationLon").toString());
    System.out.println("Odometer: " + d.get("odometer").toString());
    System.out.println("Orientation: " + d.get("currentOrientation").toString());
    System.out.println("Congestion level: " + d.get("congestionLevel").toString());
    System.out.println("Status: " + d.get("status").toString());
    System.out.println("Label: " + d.get("label").toString());
    System.out.println("License plate: " + d.get("licensePlate").toString());
    System.out.println("**********************************\n");
}