Example usage for com.mongodb MongoClient getDB

List of usage examples for com.mongodb MongoClient getDB

Introduction

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

Prototype

@Deprecated 
public DB getDB(final String dbName) 

Source Link

Document

Gets a database object.

Usage

From source file:Publisher.java

public static void main(String[] args) throws UnknownHostException, InterruptedException {
    System.out.println("testing publisher");

    //System.out.println(tripUpdates.get(0).tripId + " - " + 
    //      tripUpdates.get(0).stopUpdates.get(0).stopSeq + ": " + 
    //   tripUpdates.get(0).stopUpdates.get(0).arrivalDelay);

    GtfsRealtimeHttpVehicleUpdateSource gtfs = new GtfsRealtimeHttpVehicleUpdateSource();
    GtfsRealtimeHttpTripUpdateSource tripgtfs = new GtfsRealtimeHttpTripUpdateSource();
    //GtfsRealtimeHttpAlertsUpdateSource alertgtfs = new GtfsRealtimeHttpAlertsUpdateSource();

    Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>();
    jedisClusterNodes.add(new HostAndPort("193.10.227.200", 7005));
    JedisCluster jc = new JedisCluster(jedisClusterNodes);
    System.out.println("Currently " + jc.getClusterNodes().size() + " nodes in cluster");
    Map<String, JedisPool> nodeMap = jc.getClusterNodes();
    List<JedisPool> nodePoolList = new ArrayList<JedisPool>(nodeMap.values());
    Collections.shuffle(nodePoolList);

    System.out.println("connect to mongo");
    ;//from   w  w w. j  av  a2  s. c om
    MongoClient mongoClient = new MongoClient("192.168.93.73");
    DB db = mongoClient.getDB("mydb");
    System.out.println("connected!");
    DBCollection coll = db.getCollection("testCollection");

    /*String ID = "test";
          BasicDBObject doc = new BasicDBObject();
          doc.append("_id", ID).append("seq", "0");
          System.out.println("find");
          DBCursor cursor = coll.find();
          System.out.println("print");
           while(cursor.hasNext()) {
     System.out.println(cursor.next());
             }*/
    while (true) {
        List<TripUpdate> tripUpdates;
        List<AlertTest> alerts = null;
        TripUpdateTest tpt = new TripUpdateTest();
        if (args.length > 0 && args[0].equals("testArrivalDelay"))
            tripUpdates = tpt.testArrivalDelay("testTrip");
        else if (args.length > 0 && args[0].equals("testDepartureDelay"))
            tripUpdates = tpt.testDepartureDelay("testTrip");
        else if (args.length > 0 && args[0].equals("tesTripUpdate"))
            tripUpdates = tpt.testTripUpdate("testTrip");
        else if (args.length > 0 && args[0].equals("testDepArrDelay"))
            tripUpdates = tpt.testDepArrDelay("testTrip", "testTrip2");
        else if (args.length > 0 && args[0].equals("testRandomDelay"))
            tripUpdates = tpt.testRandomDelay("testTrip", "testTrip2");
        else if (args.length > 0 && args[0].equals("testFileDelay"))
            tripUpdates = tpt.testFileDelay("testTrip");
        else
            tripUpdates = tripgtfs.getTripUpdates();

        if (args.length > 0 && (args[0].equals("testAlert") || args.length > 1 && args[1].equals("testAlert")))
            alerts = tpt.testAlert("testRoute");

        //List<Alert> alerts = alertgtfs.getAlerts();
        List<VehiclePositionTest> positions = gtfs.getVehiclePositionUpdates();

        correctNullValues(positions);
        //printPositions(positions);

        if (args.length > 0 && args[0].equals("mongo")) {
            mongoInsertTest(db, positions);
            mongoQueryTest(db, positions);
            mongoInsertTest2(db, tripUpdates);
            mongoQueryTest2(db, tripUpdates);
        }

        //redisInsertTest(jc, positions);
        //redisQueryTest(jc, positions);
        //redisInsertTest2(jc, tripUpdates);
        //redisInsertTest3(jc, alerts);
        //redisQueryTest2(jc, tripUpdates);
        pubTest(nodePoolList, positions);
        pubTest2(nodePoolList, tripUpdates);
        if (alerts != null)
            pubTest3(nodePoolList, alerts);
        Thread.sleep(10000);
    }
}

From source file:ImportCSVtoMongo.java

public void run() {
    String csvFile = "";

    BufferedReader br = null;/*from  w ww .  ja  v  a 2 s .  c  o m*/

    String line = "";

    String csvSplitBy = "##";

    Scanner io = new Scanner(System.in);

    int i = 1;
    try {
        MongoClient mongo = new MongoClient("localhost", 27017);
        DB db = mongo.getDB("doep");
        //
        System.out.println("Enter the csv file path");
        csvFile = io.next();

        System.out.println("Enter the collection name you want to create");
        String coll_name = io.next();

        db.createCollection(coll_name, null);

        DBCollection coll = db.getCollection(coll_name);
        System.out.println("Connected to Collection");

        BasicDBObject doc = new BasicDBObject();

        br = new BufferedReader(new FileReader(csvFile));

        int q_no = 1;
        while ((line = br.readLine()) != null) {

            //use comma seperated values
            String[] question = line.split(csvSplitBy);
            System.out.println(i++);
            System.out.println("Question:" + question[1]);
            System.out.println("Option1:" + question[2]);
            System.out.println("Option2:" + question[3]);
            System.out.println("Option3:" + question[4]);
            System.out.println("Option4:" + question[5]);
            System.out.println("ANSWER: " + question[6]);
            System.out.println();

            doc.put("qid", coll_name + q_no);
            doc.put("quest", question[1]);
            doc.put("op1", question[2]);
            doc.put("op2", question[3]);
            doc.put("op3", question[4]);
            doc.put("op4", question[5]);
            doc.put("ans", question[6]);
            doc.put("image", "none");

            coll.insert(doc);
            q_no++;
            doc.clear();

        }
    } catch (UnknownHostException ex) {
        Logger.getLogger(ImportCSVtoMongo.class.getName()).log(Level.SEVERE, null, ex);
    } catch (FileNotFoundException e) {

    } catch (IOException e) {

    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}

From source file:Session_details.java

public Session_details() {
    initComponents();//  w  w  w  .ja va2  s  . c  o m

    try {

        String str = "stats", str2;
        MongoClient mc = new MongoClient("localhost", 27017);
        DB db = mc.getDB("parking_system");
        DBCollection collection = db.getCollection("statistics");

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

        DBCursor cursor = collection.find(field);

        while (cursor.hasNext()) {
            DBObject obj = (DBObject) cursor.next();
            str2 = obj.get("_id").toString();

            if (str2.equals(str)) {
                wheeler2_cnt_field.setText(obj.get("2wheeler_count").toString());
                wheeler2_income_field.setText(obj.get("2wheeler_income").toString());
                wheeler4_cnt_field.setText(obj.get("4wheeler_count").toString());
                wheeler4_income_field.setText(obj.get("4wheeler_income").toString());
                other_cnt_field.setText(obj.get("other_count").toString());
                other_income_field.setText(obj.get("other_income").toString());
                time_field.setText(obj.get("time").toString());

                break;

            }

        }

    } catch (Exception e) {
        System.err.println(e);
    }

}

From source file:Session_details.java

public void updateSession(String s1)//, String s2)
{
    try {//from  w ww . j a  va2  s  . com

        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 {//from   w w w.j  a va 2 s  .  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

public admin_update_Emp(String userid_new2) {
    initComponents();//  w  w  w  .j  ava2s .  co m

    //userid_new = admin_change.userid;
    userid_new = userid_new2;

    try {

        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(field);

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

            if (userid_db.equals(userid_new)) {
                old_name_field.setText(obj.get("name").toString());
                old_lname_field.setText(obj.get("lname").toString());
                old_mobno_field.setText(obj.get("mobileno").toString());
                old_userid_field.setText(obj.get("_id").toString());
                old_password_field.setText(obj.get("password").toString());
                System.out.println("In if");
                break;

            }

        }

    } catch (Exception e) {

    }

}

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 {//  ww w .  java 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:home.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./*w  w  w .  ja v a 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 out = response.getWriter();
    response.setContentType("text/html");
    String username = request.getParameter("username");

    String tweet = request.getParameter("tweet");
    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");

        DBCursor cursor = Tweet.find();
        int count = 0;
        if (cursor.size() < 1) {
            count = 1;
        } else {
            count = cursor.size() + 1;

        }
        BasicDBObject document = new BasicDBObject();
        document.put("tweet_id", count);
        document.put("username", username);
        document.put("tweet_text", tweet);
        document.put("date", new Date());
        Tweet.insert(document);

        ArrayList<Integer> tweet_id = new ArrayList();
        tweet_id.add(count);
        DBObject searchObject = new BasicDBObject();
        searchObject.put("username", username);

        DBObject modifiedObject = new BasicDBObject();
        modifiedObject.put("$push", new BasicDBObject().append("tweet_id", count));
        User.update(searchObject, modifiedObject);

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

From source file:Veehicle_entry.java

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

    try {/*from   ww w  .j  av a2 s.c  o m*/

        String name, vehno, mobno;

        Random rand = new Random();
        token = rand.nextInt(10000);

        name = Name_field.getText();

        mobno = mobno_field.getText();

        vehno = vehno_strt1.getSelectedItem().toString() + "-" + vehno_strt2.getText() + "-"
                + vehno_strt3.getSelectedItem().toString() + "-" + vehno_strt4.getText();

        //System.out.println(vehno);

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

        DBCollection collection1 = db.getCollection("vehicle_count");
        DBCollection collection2 = db.getCollection("statistics");

        if (veh_count == 2) {

            vehno = vehno + "W2";

            BasicDBObject queryx = new BasicDBObject();
            BasicDBObject fieldx = new BasicDBObject();
            fieldx.put("vehno", 1);
            DBCursor cursorx = collection.find(queryx, fieldx);

            while (cursorx.hasNext()) {

                BasicDBObject objx = (BasicDBObject) cursorx.next();

                String temp;
                temp = objx.getString("vehno");
                if (temp.equals(vehno)) {
                    JOptionPane.showMessageDialog(null, "vehicle number already exits!\n Entry Rejected!");
                    exists = 1;
                    setVisible(false);
                    new Veehicle_entry(veh_count).setVisible(true);

                }
            }

            if (exists == 0) {
                int count_bike;

                BasicDBObject doc = new BasicDBObject("_id", token).append("name", name)
                        .append("mobileno", mobno).append("vehno", vehno).append("intime", time_mills);

                validator = validate(collection, doc);
                if (validator == 1) {
                    //Updating vehicle count....
                    BasicDBObject query = new BasicDBObject();
                    BasicDBObject field = new BasicDBObject();
                    field.put("wheeler_2", 1);
                    DBCursor cursor = collection1.find(query, field);

                    BasicDBObject obj = (BasicDBObject) cursor.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);

                    //updating session...
                    int cnt_bike;
                    BasicDBObject query2 = new BasicDBObject();
                    BasicDBObject field2 = new BasicDBObject();
                    field2.put("2wheeler_count", 1);
                    DBCursor cursor2 = collection2.find(query2, field2);

                    BasicDBObject obj2 = (BasicDBObject) cursor2.next();
                    cnt_bike = obj2.getInt("2wheeler_count");
                    System.out.println(cnt_bike);

                    BasicDBObject searchUpdate2 = new BasicDBObject();
                    searchUpdate2.append("2wheeler_count", cnt_bike);

                    BasicDBObject UpdateObject2 = new BasicDBObject();
                    UpdateObject2.append("$set", new BasicDBObject("2wheeler_count", cnt_bike + 1));

                    collection2.update(searchUpdate2, UpdateObject2);

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

        else if (veh_count == 4) {

            vehno = vehno + "W4";

            BasicDBObject queryx = new BasicDBObject();
            BasicDBObject fieldx = new BasicDBObject();
            fieldx.put("vehno", 1);
            DBCursor cursorx = collection.find(queryx, fieldx);

            while (cursorx.hasNext()) {

                BasicDBObject objx = (BasicDBObject) cursorx.next();

                String temp;
                temp = objx.getString("vehno");
                if (temp.equals(vehno)) {
                    JOptionPane.showMessageDialog(null, "vehicle number already exits!\n Entry Rejected!");
                    exists = 1;
                    setVisible(false);
                    new Veehicle_entry(veh_count).setVisible(true);

                }
            }

            if (exists == 0) {
                int count_car;

                BasicDBObject doc = new BasicDBObject("_id", token).append("name", name)
                        .append("mobileno", mobno).append("vehno", vehno).append("intime", time_mills);

                validator = validate(collection, doc);
                if (validator == 1) {

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

                    BasicDBObject obj = (BasicDBObject) cursor.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);

                    //updating session...
                    int cnt_car;
                    BasicDBObject query2 = new BasicDBObject();
                    BasicDBObject field2 = new BasicDBObject();
                    field2.put("4wheeler_count", 1);
                    DBCursor cursor2 = collection2.find(query2, field2);

                    BasicDBObject obj2 = (BasicDBObject) cursor2.next();
                    cnt_car = obj2.getInt("4wheeler_count");
                    System.out.println(cnt_car);

                    BasicDBObject searchUpdate2 = new BasicDBObject();
                    searchUpdate2.append("4wheeler_count", cnt_car);

                    BasicDBObject UpdateObject2 = new BasicDBObject();
                    UpdateObject2.append("$set", new BasicDBObject("4wheeler_count", cnt_car + 1));

                    collection2.update(searchUpdate2, UpdateObject2);

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

        else {
            vehno = vehno + "W6";

            BasicDBObject queryx = new BasicDBObject();
            BasicDBObject fieldx = new BasicDBObject();
            fieldx.put("vehno", 1);
            DBCursor cursorx = collection.find(queryx, fieldx);

            while (cursorx.hasNext()) {

                BasicDBObject objx = (BasicDBObject) cursorx.next();

                String temp;
                temp = objx.getString("vehno");
                if (temp.equals(vehno)) {
                    JOptionPane.showMessageDialog(null, "vehicle number already exits!\n Entry Rejected!");
                    exists = 1;
                    setVisible(false);
                    new Veehicle_entry(veh_count).setVisible(true);

                }
            }
            if (exists == 0) {

                int count_other;

                BasicDBObject doc = new BasicDBObject("_id", token).append("name", name)
                        .append("mobileno", mobno).append("vehno", vehno).append("intime", time_mills);

                validator = validate(collection, doc);
                if (validator == 1) {

                    System.out.println("Came 1");
                    BasicDBObject query = new BasicDBObject();
                    BasicDBObject field = new BasicDBObject();
                    field.put("other", 1);
                    DBCursor cursor = collection1.find(query, field);

                    BasicDBObject obj = (BasicDBObject) cursor.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);

                    //updating session...
                    int cnt_other;
                    BasicDBObject query2 = new BasicDBObject();
                    BasicDBObject field2 = new BasicDBObject();
                    field2.put("other_count", 1);
                    DBCursor cursor2 = collection2.find(query2, field2);

                    BasicDBObject obj2 = (BasicDBObject) cursor2.next();
                    cnt_other = obj2.getInt("other_count");
                    System.out.println(cnt_other);

                    BasicDBObject searchUpdate2 = new BasicDBObject();
                    searchUpdate2.append("other_count", cnt_other);

                    BasicDBObject UpdateObject2 = new BasicDBObject();
                    UpdateObject2.append("$set", new BasicDBObject("other_count", cnt_other + 1));

                    collection2.update(searchUpdate2, UpdateObject2);

                    setVisible(false);
                    new Entry_OR_Exit().setVisible(true);
                }
            }
        }
    } catch (Exception e) {

    }

}

From source file:JavaSimpleExample.java

License:MIT License

public static void main(String[] args) throws UnknownHostException {

    // Create seed data

    final BasicDBObject[] seedData = createSeedData();

    // Standard URI format: mongodb://[dbuser:dbpassword@]host:port/dbname

    MongoClientURI uri = new MongoClientURI("mongodb://user:pass@host:port/db");
    MongoClient client = new MongoClient(uri);
    DB db = client.getDB(uri.getDatabase());

    /*//from w  ww  .j  a  va  2 s.  c  om
     * First we'll add a few songs. Nothing is required to create the
     * songs collection; it is created automatically when we insert.
     */

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

    // Note that the insert method can take either an array or a document.

    songs.insert(seedData);

    /*
     * Then we need to give Boyz II Men credit for their contribution to
     * the hit "One Sweet Day".
     */

    BasicDBObject updateQuery = new BasicDBObject("song", "One Sweet Day");
    songs.update(updateQuery,
            new BasicDBObject("$set", new BasicDBObject("artist", "Mariah Carey ft. Boyz II Men")));

    /*
     * Finally we run a query which returns all the hits that spent 10 
     * or more weeks at number 1.
     */

    BasicDBObject findQuery = new BasicDBObject("weeksAtOne", new BasicDBObject("$gte", 10));
    BasicDBObject orderBy = new BasicDBObject("decade", 1);

    DBCursor docs = songs.find(findQuery).sort(orderBy);

    while (docs.hasNext()) {
        DBObject doc = docs.next();
        System.out.println("In the " + doc.get("decade") + ", " + doc.get("song") + " by " + doc.get("artist")
                + " topped the charts for " + doc.get("weeksAtOne") + " straight weeks.");
    }

    // Since this is an example, we'll clean up after ourselves.

    songs.drop();

    // Only close the connection when your app is terminating

    client.close();
}