List of usage examples for com.mongodb BasicDBObject get
public Object get(final String key)
From source file:com.entrib.mongoslate.visitor.stmt.translator.select.SelectStatementTranslator.java
License:Apache License
/** * Distinct keyword if used gets ignored. * * For the select_clause find all the column names where aggr function is used * //from w w w. ja va 2s . c o m * if alias clause is used * * @param selectStatement * @return */ private Aggregation buildMongoAggregation(SelectStatement selectStatement) { Aggregation aggregation = new Aggregation(); BasicDBObject aggregate = new BasicDBObject(); aggregate.append(AGGREGATE, selectStatement.getCollection().getValueAsString()); BasicDBList pipeline = new BasicDBList(); aggregate.append(PIPELINE, pipeline); Expression whereClause = selectStatement.getLogicalExpression(); if (whereClause != null) { BasicDBObject query = (BasicDBObject) selectStatement.getLogicalExpression() .accept(expressionTranslatorVisitor); pipeline.add(new BasicDBObject("$match", query)); } // if group by clause is used, go through tag list expressions // through each tag and prepare the $group pipeline object LimitExpression limitExpression = selectStatement.getLimitExpression(); if (limitExpression != null) { BasicDBObject skipAndLimit = (BasicDBObject) limitExpression.accept(expressionTranslatorVisitor); pipeline.add(new BasicDBObject("$skip", ((IntValue) skipAndLimit.get("skip")).getValueAsObject())); pipeline.add(new BasicDBObject("$limit", ((IntValue) skipAndLimit.get("skip")).getValueAsObject())); } return aggregation; }
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(); if (uiDoc == null) { log.warn("UI not found {}", accessToken); return null; }/*from w ww . j a v a2 s . c om*/ 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; }
From source file:com.flyingdonut.implementation.helpers.MongoDbConsumerAssociationStore.java
License:Apache License
private void mongoEnsureIndex(BasicDBObject query, BasicDBObject sort) { if (query.containsField(id) || query.keySet().isEmpty()) { return;//from w w w .ja v a 2s. c o m } DBObject indexDoc = new BasicDBObject(); for (String k : query.keySet()) { if (k.startsWith("$")) { mongoEnsureIndexAddAll(indexDoc, (BasicDBList) query.get(k)); } else { indexDoc.put(k, 1); } } if (sort != null) { for (String k : sort.keySet()) { indexDoc.put(k, sort.get(k)); } } getMongoDBConnection().getCollection(getCollectionName()).ensureIndex(indexDoc); }
From source file:com.foodtruckdata.mongodb.UsersInput.java
@Override public String AddTruck(String title, String logo_img, String menu_img, String phone, String email, String password) {// ww w. j a v a2 s . c om BasicDBObject document = new BasicDBObject(); document.put("title", title); document.put("phone", phone); document.put("email", email); document.put("Schedules", (new Object[] {})); document.put("Followers", (new Object[] {})); document.put("Ratings", (new Object[] {})); document.put("password", password); DBCollection coll = mongoDB.getCollection("Trucks"); coll.insert(document); ObjectId truck_id = (ObjectId) document.get("_id"); if (logo_img != null && logo_img != "") { this.storeFile(logo_img, title, truck_id); } if (menu_img != null && menu_img != "") { this.storeFile(menu_img, title, truck_id); } return truck_id.toString(); }
From source file:com.foodtruckdata.mongodb.UsersInput.java
@Override public String AddUser(String firstName, String lastName, String email, Double lat_h, Double lng_h, Double lat_w, Double lng_w) {/*from w w w. j a v a 2 s .co m*/ BasicDBObject document = new BasicDBObject(); document.put("FirstName", firstName); document.put("LastName", lastName); document.put("Email", email); document.put("Latitude_home", lat_h); document.put("Longitude_home", lng_h); document.put("Latitude_work", lat_w); document.put("Longitude_work", lng_w); DBCollection coll = mongoDB.getCollection("Users"); coll.insert(document); ObjectId user_id = (ObjectId) document.get("_id"); return user_id.toString(); }
From source file:com.foodtruckdata.mongodb.UsersInput.java
@Override public void AddSchedule(Date dateTime, String address, String truck_id) { //remove all outdated schedules BasicDBObject filter = new BasicDBObject("_id", new ObjectId(truck_id)); DBCollection coll = mongoDB.getCollection("Trucks"); Cursor cursor = coll.find(filter); if (cursor.hasNext()) { BasicDBObject truck = (BasicDBObject) cursor.next(); BasicDBObject[] schedules = (BasicDBObject[]) truck.get("Schedules"); for (BasicDBObject schedule : schedules) { if (((Date) schedule.get("Time")).compareTo(new Date()) < 0) { coll.update(filter, (new BasicDBObject("$pull", (new BasicDBObject("Schedules", schedule))))); }/* www. jav a 2 s. co m*/ } } //insert the new schedule if (dateTime.compareTo((new Date())) > 0) { BasicDBObject schedule = new BasicDBObject(); schedule.put("Time", dateTime); schedule.put("Address", address); //put lat long also coll.update(filter, (new BasicDBObject("$addToSet", (new BasicDBObject("Schedules", schedule))))); } }
From source file:com.fpt.xml.hth.db.lib.converter.MovieTheaterSessionConverter.java
/** * To convert from BasicDBObject object to MovieDBTheaterSessionDTO object * * @param object/*w w w .jav a 2s .c o m*/ * @return MovieDBTheaterSessionDTO */ //TODO: remove public MovieTheaterSessionDTO convertBasicObjectToModel(BasicDBObject object) { MovieTheaterSessionDTO dto = new MovieTheaterSessionDTO(); ObjectId id = object.getObjectId("_id"); BasicDBObject basicMovie = (BasicDBObject) object.get("movie"); BasicDBList basicLstSession = (BasicDBList) object.get("theaters"); //convert basicMovie object movie MovieDB movie = movieConverter.convertBasicObjectToModel(basicMovie); //convert basicLstSession to theaters List<TheaterSessionDTO> theaters = new ArrayList<TheaterSessionDTO>(); if (basicLstSession != null && !basicLstSession.isEmpty()) { for (int i = 0; i < basicLstSession.size(); i++) { BasicDBObject basic = (BasicDBObject) basicLstSession.get(i); TheaterSessionDTO theaterSessionDTO = tsConverter.convertBasicObjectToModel(basic); theaters.add(theaterSessionDTO); } } //set value for object MovieTheaterSessionDTO dto.setId(id); dto.setMovie(movie); dto.setTheaters(theaters); return dto; }
From source file:com.fpt.xml.hth.db.lib.converter.TheaterSessionConverter.java
/** * To convert from BasicDBObject object to TheaterDBSessionDTO object * * @param object//from w ww .ja v a2 s . co m * @return TheaterDBSessionDTO */ public TheaterSessionDTO convertBasicObjectToModel(BasicDBObject object) { String cinemaName = object.getString("cinemaName"); String id = object.getString("id"); BasicDBObject basicTheater = (BasicDBObject) object.get("theater"); BasicDBList basicSessions = (BasicDBList) object.get("sessions"); // convert basicTheater to theater TheaterDB theater = conveter.convertBasicObjectToModel(basicTheater); // convert basicSessions to session List<String> sessions = new ArrayList<String>(); for (int i = 0; i < basicSessions.size(); i++) { String session = basicSessions.get(i).toString(); sessions.add(session); } TheaterSessionDTO theaterSessionDTO = new TheaterSessionDTO(); theaterSessionDTO.setId(id); theaterSessionDTO.setCinemaName(cinemaName); theaterSessionDTO.setLstSession(sessions); theaterSessionDTO.setTheater(theater); return theaterSessionDTO; }
From source file:com.geeksanon.SessionDAO.java
License:Open Source License
/** * Insert the session ID and username in the sessions collection, where the * ID session ID generated is 32bytes.//from w w w . j a v a 2 s . c om * * @param string * @return */ public String startSession(String username) { SecureRandom secureRandom = new SecureRandom(); byte randomBytes[] = new byte[32]; secureRandom.nextBytes(randomBytes); BASE64Encoder encoder = new BASE64Encoder(); String sessionID = encoder.encode(randomBytes); BasicDBObject session = new BasicDBObject("username", username).append("_id", sessionID); sessionsCollection.insert(session); return session.get("_id").toString(); }
From source file:com.github.camellabs.iot.cloudlet.document.driver.mongodb.MongoQueryBuilder.java
License:Apache License
private void addRestriction(BasicDBObject query, String propertyWithOperator, String propertyOperator, String operator, Object value) { String property = propertyWithOperator.replaceAll(propertyOperator + "$", ""); if (query.containsField(property)) { BasicDBObject existingRestriction = (BasicDBObject) query.get(property); existingRestriction.put(operator, value); } else {//from ww w .ja v a 2 s. c o m query.put(property, new BasicDBObject(operator, value)); } }