List of usage examples for com.mongodb.client MongoCollection find
FindIterable<TDocument> find(ClientSession clientSession);
From source file:com.cognitive.cds.invocation.mongo.WorkProductDao.java
License:Apache License
public WorkProductWrapper getWorkProduct(String id) { WorkProductWrapper wpw = null;/* w w w . j av a 2s . c o m*/ mongoDbDao.setDatabase("work"); MongoCollection<Document> collection = mongoDbDao.getCollection("work"); Document filter = new Document(); filter.put("_id", new ObjectId(id)); Document obj = collection.find(filter).first(); if (obj != null) { try { String json = obj.toJson(); wpw = (WorkProductWrapper) JsonUtils.getMapper().readValue(json, WorkProductWrapper.class); } catch (IOException e) { logger.error("========> Deserialize: " + e.toString()); } } return wpw; }
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 ww w .j a v 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 w w.j a v a 2 s. com 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; }
From source file:com.cognitive.cds.invocation.mongo.WorkProductDao.java
License:Apache License
public UpdateResult deleteWorkProductAssignment(String workProductId, String userId) { WorkProductWrapper wpw;/*from w w w .j a v a2s . c o m*/ mongoDbDao.setDatabase("work"); MongoCollection<Document> collection = mongoDbDao.getCollection("work"); Document filter = new Document(); filter.put("_id", new ObjectId(workProductId)); 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(); while (iterator.hasNext()) { WorkProductAssignment workProductAssignment = (WorkProductAssignment) iterator.next(); // find the assignment and remove it from the workproduct assignments list if (workProductAssignment.getUser().getId().equalsIgnoreCase(userId) && workProductAssignment.getWorkProductId().equalsIgnoreCase(workProductId)) { iterator.remove(); } } String objectJson = JsonUtils.getMapper().writeValueAsString(wpw); Document doc = Document.parse(objectJson); doc.put("_id", new ObjectId(workProductId)); // Update the work product. result = mongoDbDao.getCollection("work").replaceOne(filter, doc); } catch (IOException e) { logger.error("========> Deserialize: " + e.toString()); } } return result; }
From source file:com.cognitive.cds.invocation.mongo.WorkProductSubscriptionDao.java
License:Apache License
/** * Call the server to fetch the engine/*from w w w . j a va 2s. c om*/ * * @param user * @return */ public WorkProductSubscription getWorkProductSubscription(String user) { WorkProductSubscription wps = null; mongoDbDao.setDatabase("work"); MongoCollection<Document> collection = mongoDbDao.getCollection("subscriptions"); logger.info("Subscription Count: " + collection.count()); // max of one // per user... Document filter = new Document(); filter.put("user", user); Document obj = collection.find(filter).first(); if (obj != null) { try { String json = obj.toJson(); wps = (WorkProductSubscription) JsonUtils.getMapper().readValue(json, WorkProductSubscription.class); } catch (IOException e) { logger.error("========> Deserialize: " + e.toString()); } } // else { // return defaulted response, or simply return null, which means // "all". // } return wps; }
From source file:com.DA.assignment1.Query.Demonstrate.java
public static void selectMovie(MongoCollection<Document> rating) throws IOException { System.out.println("Please enter userID: you can check the rating records "); BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in)); String userID = br1.readLine(); FindIterable<Document> movies = rating.find(new Document("userID", userID)); movies.forEach(new Block<Document>() { @Override/*from ww w. ja va2s. com*/ public void apply(final Document document) { System.out.println(document); } }); }
From source file:com.DA.assignment1.Query.Demonstrate.java
public static void Tag(MongoCollection<Document> tag) throws IOException { System.out.println("Please enter movieID:"); BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in)); String movieID = br1.readLine(); FindIterable<Document> tags = tag.find(new Document("movieID", movieID)); tags.forEach(new Block<Document>() { @Override//from w ww .ja v a 2 s .c o m public void apply(final Document document) { System.out.println(document); } }); }
From source file:com.eclipsesource.connect.persistence.MongoStorage.java
License:Open Source License
@Override public <T> List<T> findAll(Query<T> query) { checkArgument(query != null, "Query must not be null"); MongoDatabase db = factory.getDB();/*w ww. j a va 2 s . c om*/ MongoCollection<Document> collection = db.getCollection(query.getPlace()); List<T> result = new ArrayList<>(); for (Document document : prepareIterable(query, collection.find(createDocument(query)))) { result.add(deserializer.deserialize(document.toJson(), query.getType())); } return result; }
From source file:com.eclipsesource.connect.test.util.persistence.InMemoryStorage.java
License:Open Source License
@Override public <T> List<T> findAll(Query<T> query) { checkArgument(query != null, "Query must not be null"); MongoCollection<Document> collection = db.getCollection(query.getPlace()); List<T> result = new ArrayList<>(); for (Document document : prepareIterable(query, collection.find(createDocument(query)))) { result.add(serialization.deserialize(document.toJson(), query.getType())); }/*from w ww.j a va 2 s. c o m*/ return result; }
From source file:com.epam.dlab.auth.dao.UserInfoDAOMongoImpl.java
License:Apache License
@Override public UserInfo getUserInfoByAccessToken(String accessToken) { BasicDBObject uiSearchDoc = new BasicDBObject(); uiSearchDoc.put("_id", accessToken); MongoCollection<BasicDBObject> mc = ms.getCollection("security", BasicDBObject.class); FindIterable<BasicDBObject> res = mc.find(uiSearchDoc); BasicDBObject uiDoc = res.first();// ww w .j a v a2s. c o m if (uiDoc == null) { log.warn("UI not found {}", accessToken); return null; } Date lastAccess = uiDoc.getDate("expireAt"); if (inactiveUserTimeoutMsec < Math.abs(new Date().getTime() - lastAccess.getTime())) { log.warn("UI for {} expired but were not evicted from DB. Contact MongoDB admin to create expireable " + "index" + " on 'expireAt' key.", accessToken); this.deleteUserInfo(accessToken); return null; } String name = uiDoc.get("name").toString(); String firstName = uiDoc.getString("firstName", ""); String lastName = uiDoc.getString("lastName", ""); String remoteIp = uiDoc.getString("remoteIp", ""); BasicDBList roles = (BasicDBList) uiDoc.get("roles"); Boolean awsUser = uiDoc.getBoolean("awsUser", false); UserInfo ui = new UserInfo(name, accessToken); ui.setFirstName(firstName); ui.setLastName(lastName); ui.setRemoteIp(remoteIp); ui.setAwsUser(awsUser); Object awsKeys = uiDoc.get("awsKeys"); if (awsKeys != null) { ((BasicDBObject) awsKeys).forEach((key, val) -> ui.addKey(key, val.toString())); } roles.forEach(o -> ui.addRole("" + o)); log.debug("Found persistent {}", ui); return ui; }