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:com.tengen.Week3Hw3_1.java

License:Apache License

public static void main(String[] args) throws UnknownHostException {
    MongoClient client = new MongoClient();

    DB database = client.getDB("school");
    DBCollection collection = database.getCollection("students");

    DBCursor cursor = collection.find();
    int student_id = -1;
    int count = 0;

    try {/*  w  ww. jav  a  2s.  c  o m*/
        while (cursor.hasNext()) {
            DBObject student = cursor.next();
            //student.grades;
            BasicDBObject searchQuery = new BasicDBObject().append("_id", student.get("_id"));
            BasicDBList scores = (BasicDBList) student.get("scores");
            BasicDBObject[] scoresArr = scores.toArray(new BasicDBObject[0]);
            double score = 100.0;
            BasicDBObject rem = new BasicDBObject();
            for (BasicDBObject dbObj : scoresArr) {
                String type = dbObj.get("type").toString();
                if (type.equals("homework")) {
                    double s = Double.parseDouble(dbObj.get("score").toString());

                    if (score > s) {
                        score = s;
                        rem = dbObj;
                    }
                }
            }
            BasicDBObject update = new BasicDBObject("scores", rem);
            collection.update(searchQuery, new BasicDBObject("$pull", update));
        }
    } finally {
        cursor.close();
    }
}

From source file:com.terkaly.JavaMongoDB.App.java

License:Open Source License

public static void main(String[] args) {
    try {//www  .  j ava  2s. c o  m
        // Create a connection using mongoClient
        // 23.99.88.154 is obtained from the portal
        MongoClient mongoClient = new MongoClient("[ put your ip address here ]", 27017);

        // Get a connection to mydb
        DB db = mongoClient.getDB("mydb");

        // mydb has one or more collections, get "testCollection"
        DBCollection collection = db.getCollection("testCollection");

        // Create an empty object
        BasicDBObject empty = new BasicDBObject();

        // Clear out testCollection
        collection.remove(empty);

        // Acknowledges the write operation only
        // after committing the data to the journal
        mongoClient.setWriteConcern(WriteConcern.JOURNALED);

        // Here is the data format in JSON
        // {
        //   "name": "MongoDB",
        //   "type": "database",
        //   "count": 1,
        //   "info": {
        //         "x": 203,
        //         "y": 102
        //    }
        // }

        BasicDBObject doc = new BasicDBObject("name", "MongoDB").append("type", "database").append("count", 1)
                .append("info", new BasicDBObject("x", 203).append("y", 102));
        collection.insert(doc);

        DBObject myDoc = collection.findOne();
        System.out.println(myDoc);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.timboudreau.netbeans.mongodb.OneDBChildren.java

License:Open Source License

@Override
protected boolean createKeys(final List<CollectionInfo> list) {
    ConnectionProblems problems = lookup.lookup(ConnectionProblems.class);
    problems.invoke(new Callable<Void>() {
        @Override//w  w w  . j a v  a2 s . c om
        public Void call() throws Exception {
            MongoClient client = lookup.lookup(MongoClient.class);
            DbInfo info = lookup.lookup(DbInfo.class);
            DB db = client.getDB(info.dbName);
            List<String> names = new LinkedList<>(db.getCollectionNames());
            for (String name : names) {
                list.add(new CollectionInfo(name, lookup));
            }
            return null;
        }
    });
    return true;
}

From source file:com.tml.pathummoto.Dao.BillDao.java

public ArrayList<Bill> searchBill(String no) {

    MongoClient mongoClient = new MongoClient("localhost", 27017);

    // Now connect to your databases
    DB db = mongoClient.getDB("pathumdb");
    System.out.println("Connect to database successfully");

    DBCollection coll = db.getCollection("Bill");
    System.out.println("Collection user selected successfully");

    BasicDBObject whereQuery = new BasicDBObject();
    whereQuery.put("vehicleNo", no);
    DBCursor cursor = coll.find(whereQuery);

    ArrayList<Bill> bills = new ArrayList<Bill>();

    while (cursor.hasNext()) {
        BasicDBObject doc = (BasicDBObject) cursor.next();

        if (doc != null) {
            Bill bill = new Bill();
            bill.setDate(doc.getDate("Date"));
            bill.setKm(doc.getInt("km"));
            bill.setPayment(doc.getInt("payment"));
            bill.setService(doc.getInt("service"));
            bill.setTotalCost(doc.getInt("total"));
            String array = "";
            int partSize = doc.getInt("partsSize");
            for (int i = 0; i < partSize; i++) {
                String parts = "part" + i;
                String quentity = "quant" + i;
                array = array + doc.getString(parts) + " : " + doc.getInt(quentity) + "\n";

            }//  ww  w.j  av  a  2  s  .  c o  m
            bill.setParts(array);
            bills.add(bill);
        }
    }
    return bills;
}

From source file:com.tml.pathummoto.Dao.Connection.java

public static DBCollection main(String args[]) {

    try {//from  ww  w .ja  va 2s  . c om

        // To connect to mongodb server
        MongoClient mongoClient = new MongoClient("localhost", 27017);

        // Now connect to your databases
        DB db = mongoClient.getDB("pathumdb");
        System.out.println("Connect to database successfully");

        DBCollection coll = db.getCollection("user");
        System.out.println("Collection mycol selected successfully");
        return coll;

    } catch (Exception e) {
        System.err.println(e.getClass().getName() + ": " + e.getMessage());
        DBCollection coll = null;
        return coll;
    }

}

From source file:com.tml.pathummoto.Dao.CustomDao.java

public void addCustomer(Customer customer) {
    MongoClient mongoClient = new MongoClient("localhost", 27017);

    // Now connect to your databases
    DB db = mongoClient.getDB("pathumdb");
    System.out.println("Connect to database successfully");

    DBCollection coll = db.getCollection("customer");
    System.out.println("Collection user selected successfully");
    BasicDBObject doc = new BasicDBObject("title", "customer").append("name", customer.getName())
            .append("address", customer.getAddress()).append("_id", customer.getVehicleNo())
            .append("vehicletype", customer.getVehicleType())
            .append("dateOfDelivery", customer.getDateOfDelivery()).append("engineNo", customer.getEngineNo())
            .append("chassisNo", customer.getChassisNo()).append("freeServiceNo", customer.getFreeServiceNo())
            .append("payment", customer.getPayment()).append("serviceNo", customer.getServiceNo())
            .append("lastKm", customer.getLastKm()).append("phoneNo", customer.getPhoneNo());

    coll.insert(doc);/*from   ww w.ja v a 2 s . c  o  m*/
    System.out.println("successfully");

}

From source file:com.tml.pathummoto.Dao.CustomDao.java

public Customer searchCustomer(String no) {
    Customer customer = new Customer();
    // To connect to mongodb server
    MongoClient mongoClient = new MongoClient("localhost", 27017);

    // Now connect to your databases
    DB db = mongoClient.getDB("pathumdb");
    System.out.println("Connect to database successfully");

    DBCollection coll = db.getCollection("customer");
    System.out.println("Collection user selected successfully");

    BasicDBObject whereQuery = new BasicDBObject();
    whereQuery.put("_id", no);
    DBCursor cursor = coll.find(whereQuery);
    while (cursor.hasNext()) {
        System.out.println(cursor.next());
    }//from  ww  w. j  a  v  a  2s. co m
    BasicDBObject doc = (BasicDBObject) cursor.curr();
    System.out.println("doc" + doc);
    if (doc != null) {
        customer.setVehicleNo(doc.getString("_id"));
        customer.setName(doc.getString("name"));
        customer.setPayment(doc.getInt("payment"));
        customer.setFreeServiceNo(doc.getInt("freeServiceNo"));
        customer.setServiceNo(doc.getInt("serviceNo"));
        customer.setDateOfDelivery(doc.getDate("dateOfDelivery"));
        customer.setLastKm(doc.getInt("lastKm"));

    }

    return customer;
}

From source file:com.tml.pathummoto.Dao.CustomDao.java

public void updateCustomer(Customer customer) {
    MongoClient mongoClient = new MongoClient("localhost", 27017);

    // Now connect to your databases
    DB db = mongoClient.getDB("pathumdb");
    System.out.println("Connect to database successfully");

    DBCollection coll = db.getCollection("customer");
    System.out.println("Collection user selected successfully");

    BasicDBObject whereQuery = new BasicDBObject();
    whereQuery.put("_id", customer.getVehicleNo());

    DBCursor cursor = coll.find(whereQuery);
    System.out.println(customer.getVehicleNo());
    DBObject query = new BasicDBObject("_id", customer.getVehicleNo());
    DBObject update = new BasicDBObject();
    update.put("$set", new BasicDBObject("payment", customer.getPayment())
            .append("freeServiceNo", customer.getFreeServiceNo()).append("serviceNo", customer.getServiceNo())
            .append("dateOfDelivery", customer.getDateOfDelivery()).append("lastKm", customer.getLastKm()));

    WriteResult result = coll.update(query, update);
    System.out.println(result);/*from w w  w.j a va2s.  c  o m*/

}

From source file:com.tml.pathummoto.Dao.CustomDao.java

public void addBill(ObservableList<Part> data, Bill bill) {
    MongoClient mongoClient = new MongoClient("localhost", 27017);

    // Now connect to your databases
    DB db = mongoClient.getDB("pathumdb");
    System.out.println("Connect to database successfully");

    DBCollection coll = db.getCollection("Bill");
    System.out.println("Collection user selected successfully");

    LocalDate now = LocalDate.now();
    Date date = java.sql.Date.valueOf(now);

    for (int i = 0; i < data.size(); i++) {

    }//  w w w. j av  a  2  s.c o m

    BasicDBObject doc = new BasicDBObject("title", "Bill").append("Date", date)
            .append("vehicleNo", bill.getVehicleNo()).append("km", bill.getKm())
            .append("payment", bill.getPayment()).append("service", bill.getService())
            .append("total", bill.getTotalCost()).append("partsSize", data.size());

    for (int i = 0; i < data.size(); i++) {
        String parts = "part" + i;
        String quentity = "quant" + i;
        doc.append(parts, data.get(i).getPartNo()).append(quentity, data.get(i).getQuentity());
    }
    coll.insert(doc);
    System.out.println(doc);
}

From source file:com.tml.pathummoto.Dao.ModelDao.java

public void addModel(Model model) {
    MongoClient mongoClient = new MongoClient("localhost", 27017);

    // Now connect to your databases
    DB db = mongoClient.getDB("pathumdb");
    System.out.println("Connect to database successfully");

    DBCollection coll = db.getCollection("model");

    BasicDBObject doc = new BasicDBObject("title", "model").append("_id", model.getModelName());

    coll.insert(doc);//w w w.  j a  v  a2 s. c  o  m
    System.out.println("Document inserted successfully");

}