List of usage examples for com.mongodb MongoClient getDatabase
public MongoDatabase getDatabase(final String databaseName)
From source file:com.averageloser.mongodemo.Main.java
public static void main(String[] args) { List<Book> books = new ArrayList(); for (int i = 0; i < 3; i++) { Book book = new Book(); book.setTitle(i + " title"); book.setDescription(i + " description"); book.setAuthor(i + " author"); book.setPublisher(i + " publisher"); books.add(book);/*from www . j a v a 2s . c o m*/ } DBHelper helper = BookDBHelper.getInstance(); MongoClient client = helper.getMongoClient(); //System.out.println("\n" + client.toString()); MongoCollection<Document> booksCollection = helper.getMongoCollection(client.getDatabase("items"), "books"); System.out.println(booksCollection.count()); //Add documents to the books collection. helper.insertDocument(booksCollection, books.get(0)); helper.insertDocument(booksCollection, books.get(1)); helper.insertDocument(booksCollection, books.get(2)); //Print size of the collection. System.out.println(booksCollection.count()); //Grab the book from the database by its title. String query = "0 title"; Book book = (Book) helper.getDocument(booksCollection, new Document("title", query)); //Print first book details. System.out.println(book.toString()); //Remove the third book from the collection and verify that it was deleted, printing collection size. helper.removeDocument(booksCollection, books.get(2)); System.out.println(booksCollection.count()); }
From source file:com.baifendian.swordfish.common.job.struct.datasource.MongoDatasource.java
License:Apache License
@Override public void isConnectable() throws Exception { MongoClient mongoClient = new MongoClient(new MongoClientURI(this.address)); try {//from www .j ava 2 s . co m MongoClientOptions options = MongoClientOptions.builder().connectTimeout(10).socketKeepAlive(false) .build(); MongoDatabase db = mongoClient.getDatabase(this.database); for (Document doc : db.listCollections()) { logger.debug("{}", doc); } } finally { mongoClient.close(); } }
From source file:com.bdnc.ecommercebdnc.dao.DAODocumentos.java
public boolean salvarCompra(Compra compra) { MongoClient client = new MongoClient("localhost", 27017); MongoDatabase dataBase = client.getDatabase("ecommerce-bdnc"); MongoCollection<Document> collection = dataBase.getCollection("vendas"); collection.insertOne(compra.toDocument()); client.close();//from w w w.ja va2 s . c o m return true; }
From source file:com.bdnc.ecommercebdnc.dao.DAODocumentos.java
public List<Compra> buscarCompras() { MongoClient client = new MongoClient("localhost", 27017); MongoDatabase dataBase = client.getDatabase("ecommerce-bdnc"); MongoCollection<Document> collection = dataBase.getCollection("vendas"); List<Compra> compras = new ArrayList<>(); MongoCursor<Document> cursor = collection.find().iterator(); while (cursor.hasNext()) { Compra compra = new Compra(); compras.add(compra.fromDocument(cursor.next())); }/* ww w .j a va 2 s . c o m*/ client.close(); return compras; }
From source file:com.chadcover.Homework_23.java
License:Apache License
public static void main(String[] args) { MongoClient client = new MongoClient(); MongoDatabase database = client.getDatabase("students"); MongoCollection<Document> collection = database.getCollection("gradesBak"); long count = collection.count(); System.out.println(count);/* w w w . j a va 2s .c om*/ Bson filter = eq("type", "homework"); Bson sort = ascending("student_id", "score"); Bson projection = fields(include("student_id", "score")); // better for large datasets MongoCursor<Document> result = collection.find(filter).sort(sort).projection(projection).iterator(); try { int lastId = 0; int counter = 0; while (result.hasNext()) { Document cur = result.next(); int id = (Integer) cur.get("student_id"); // when moving to new student // delete that record, which is the lowest homework score if (id != lastId || counter == 0) { double grade = (Double) cur.get("score"); System.out.println("Delete this score: " + grade); collection.deleteOne(eq("_id", cur.get("_id"))); } else { // do nothing } lastId = id; counter++; } } finally { result.close(); } }
From source file:com.cognitive.cds.invocation.mongo.IntentMappingDao.java
License:Apache License
public DeleteResult deleteIntent(String intentName) throws JsonProcessingException { MongoClient mongo = mongoDbDao.getMongoClient(); MongoDatabase db = mongo.getDatabase("intent"); MongoCollection<Document> collection = db.getCollection("cdsintent"); BasicDBObject query = new BasicDBObject(); query.append("name", intentName); DeleteResult result = collection.deleteOne(query); return result; }
From source file:com.cognitive.cds.invocation.mongo.IntentMappingDao.java
License:Apache License
public Document updateIntentMapping(IntentMapping im) throws JsonProcessingException { MongoClient mongo = mongoDbDao.getMongoClient(); MongoDatabase db = mongo.getDatabase("intent"); MongoCollection<Document> collection = db.getCollection("cdsintent"); Document filter = new Document(); if (im.get_id() != null) { filter.put("_id", im.get_id()); } else if (im.getId() != null && !im.getId().isEmpty()) { filter.put("_id", new ObjectId(im.getId())); } else {/*w w w .jav a 2 s.c o m*/ return null; } Document obj = collection.find(filter).first(); Document result = null; if (obj != null) { try { String objectJson = mapper.writeValueAsString(im); Document doc = Document.parse(objectJson); doc.put("_id", im.get_id()); result = collection.findOneAndReplace(filter, doc); if (cache.containsKey(im.getName())) { cache.put(im.getName(), im); } } catch (IOException e) { logger.error("========> Deserialize: " + e.toString()); } } return result; }
From source file:com.cognitive.cds.invocation.mongo.WorkProductDao.java
License:Apache License
public DeleteResult deleteWorkProduct(String id) throws JsonProcessingException { mongoDbDao.setDatabase("work"); MongoClient mongo = mongoDbDao.getMongoClient(); MongoDatabase db = mongo.getDatabase("work"); MongoCollection<Document> collection = db.getCollection("work"); BasicDBObject query = new BasicDBObject(); query.append("_id", new ObjectId(id)); DeleteResult result = collection.deleteOne(query); return result; }
From source file:com.cognitive.cds.invocation.mongo.WorkProductDao.java
License:Apache License
public UpdateResult updateWorkProduct(WorkProduct wp) throws JsonProcessingException { mongoDbDao.setDatabase("work"); MongoClient mongo = mongoDbDao.getMongoClient(); MongoDatabase db = mongo.getDatabase("work"); MongoCollection<Document> collection = db.getCollection("work"); Document filter = new Document(); if (wp.getId() == null) return null; else/*from w w w . j av a 2 s .c o m*/ filter.put("_id", new ObjectId(wp.getId())); Document obj = collection.find(filter).first(); UpdateResult result = null; if (obj != null) { try { String objectJson = JsonUtils.getMapper().writeValueAsString(wp); Document doc = Document.parse(objectJson); result = collection.updateOne(filter, new Document("$set", new Document("workproduct", doc))); } catch (IOException e) { logger.error("========> Deserialize: " + e.toString()); } } return result; }
From source file:com.cognitive.cds.invocation.mongo.WorkProductDao.java
License:Apache License
public UpdateResult updateWorkProductAssignment(WorkProductAssignment wpa) throws JsonProcessingException { mongoDbDao.setDatabase("work"); MongoClient mongo = mongoDbDao.getMongoClient(); MongoDatabase db = mongo.getDatabase("work"); MongoCollection<Document> collection = db.getCollection("work"); WorkProductWrapper wpw;/*w ww. ja va 2s. c om*/ Document filter = new Document(); if (wpa.getUser() == null) return null; else filter.put("_id", new ObjectId(wpa.getWorkProductId())); Document obj = collection.find(filter).first(); UpdateResult result = null; if (obj != null) { try { String json = obj.toJson(); wpw = (WorkProductWrapper) JsonUtils.getMapper().readValue(json, WorkProductWrapper.class); Iterator<WorkProductAssignment> iterator = wpw.getAssignments().iterator(); // remove if there is an existing work product assignment with the same user id and work product id. while (iterator.hasNext()) { WorkProductAssignment workProductAssignment = (WorkProductAssignment) iterator.next(); if (workProductAssignment.getUser().getId().equalsIgnoreCase(wpa.getUser().getId()) && workProductAssignment.getWorkProductId().equalsIgnoreCase(wpa.getWorkProductId())) { iterator.remove(); } } // add the new work product assignment wpw.getAssignments().add(wpa); String objectJson = JsonUtils.getMapper().writeValueAsString(wpw); Document doc = Document.parse(objectJson); doc.put("_id", new ObjectId(wpa.getWorkProductId())); result = mongoDbDao.getCollection("work").replaceOne(filter, doc); } catch (IOException e) { logger.error("========> Deserialize: " + e.toString()); } } return result; }