List of usage examples for com.mongodb BasicDBObjectBuilder start
public static BasicDBObjectBuilder start(final String key, final Object val)
From source file:com.example.labs.actions.data.ProjectsAction.java
License:Apache License
public String execute() throws Exception { connection = MongoDBConnection.getInstance(); connection.connect("turbines"); DBCollection turbineData = connection.getMongoCollection("turbineData"); DBObject searchQuery = new BasicDBObject(); searchQuery.put("date", BasicDBObjectBuilder.start("$gte", startDate).add("$lte", endDate).get()); DBObject keys = new BasicDBObject(); keys.put("date", "1"); //from query param if (StringUtils.isEmpty(fields)) { success = 0;/*from w ww. j a v a2 s .c o m*/ error = "Please provide valid fields"; return ERROR; } String[] fieldKeys = fields.split(","); for (String k : fieldKeys) { keys.put(k, "1"); } DBCursor cursor = turbineData.find(searchQuery, keys); DBObject sortTime = new BasicDBObject(); sortTime.put("date", 1); cursor.sort(sortTime); List<DBObject> dataPoints = cursor.toArray(); LOG.debug("Number of data points: " + dataPoints.size()); data = new ArrayList<Object>(); for (DBObject dataP : dataPoints) { data.add(dataP); } success = 1; return SUCCESS; }
From source file:com.gigaspaces.persistency.metadata.IndexBuilder.java
License:Open Source License
private void createIndex(String typeSimpleName, String routing, SpaceIndexType type, BasicDBObjectBuilder option) {//w w w . j a v a 2 s. c o m DBCollection c = client.getCollection(typeSimpleName); DBObject key; if (type == SpaceIndexType.BASIC) { key = BasicDBObjectBuilder.start(routing, "hashed").get(); } else { key = BasicDBObjectBuilder.start(routing, 1).get(); } c.createIndex(key, option.get()); }
From source file:com.google.api.ads.adwords.jaxws.extensions.report.model.persistence.mongodb.MongoEntityPersister.java
License:Open Source License
/** * @see com.google.api.ads.adwords.jaxws.extensions.report.model.persistence.EntityPersister * #get(java.lang.Class, java.lang.String, java.lang.Object, java.lang.String, * java.util.Date, java.util.Date, int, int) *//*from w w w .j av a 2 s . com*/ @Override public <T> List<T> get(Class<T> t, String key, Object value, String dateKey, Date dateStart, Date dateEnd, int numToSkip, int limit) { Map<String, Object> keyValueList = new HashMap<String, Object>(); if (key != null) { keyValueList.put(key, value); } if (dateKey != null && dateStart != null) { if (dateEnd == null) { // One day only keyValueList.put(dateKey, DateUtil.formatYearMonthDayNoDash(dateStart)); } else { // All within the date range keyValueList.put(dateKey, BasicDBObjectBuilder.start("$gte", DateUtil.formatYearMonthDayNoDash(dateStart)) .add("$lte", DateUtil.formatYearMonthDayNoDash(dateEnd)).get()); } } return get(t, keyValueList, numToSkip, limit); }
From source file:com.google.api.ads.adwords.jaxws.extensions.report.model.persistence.mongodb.MongoEntityPersister.java
License:Open Source License
@Override public <T> List<T> get(Class<T> t, String key, Object value, String dateKey, String dateStart, String dateEnd) { Map<String, Object> keyValueList = new HashMap<String, Object>(); if (key != null) { keyValueList.put(key, value);/*from w w w . j a v a 2s.c om*/ } keyValueList.put(dateKey, BasicDBObjectBuilder.start("$gte", dateStart).add("$lte", dateEnd).get()); return get(t, keyValueList); }
From source file:com.grallandco.impl.MongoCAPIBehavior.java
License:Apache License
/** * Load the documents into MongoDB// w w w.j ava 2 s. c o m * @param database * @param docs * @return */ @Override public List<Object> bulkDocs(String database, List<Map<String, Object>> docs) { DB db = MongoConnectionManager.getMongoClient().getDB(getDatabaseName(database)); List<Object> result = new ArrayList<Object>(); logger.log(Level.INFO, String.format("Replicating %d document(s)", docs.size())); for (Map<String, Object> doc : docs) { Map<String, Object> meta = (Map<String, Object>) doc.get("meta"); Map<String, Object> json = (Map<String, Object>) doc.get("json"); String base64 = (String) doc.get("base64"); if (meta == null) { // if there is no meta-data section, there is nothing we can do logger.log(Level.WARNING, "Document without meta in bulk_docs, ignoring...."); continue; } else if ("non-JSON mode".equals(meta.get("att_reason")) || "invalid_json".equals(meta.get("att_reason"))) { // optimization, this tells us the body isn't json json = new HashMap<String, Object>(); } else if (json == null && base64 != null) { // use Java 6/7 XML Base64 library // TODO : see if it makes sense to use Java8 Library java.util.Base64 String jsonValue = new String(DatatypeConverter.parseBase64Binary(base64)); DBObject o = (DBObject) JSON.parse(jsonValue); DBObject mongoJson = BasicDBObjectBuilder.start("_id", meta.get("id")).get(); // need to check if json keys do not contains . or $ and replace them with other char // TODO : Copy the doc, put _id at the top and clean key names Set<String> keys = o.keySet(); for (String key : keys) { String newKey = key; newKey = newKey.replace(".", MongoDBCouchbaseReplicator.dotReplacement); newKey = newKey.replace("$", MongoDBCouchbaseReplicator.dollarReplacement); mongoJson.put(newKey, o.get(key)); } // add meta data if configured if (MongoDBCouchbaseReplicator.keepMeta) { mongoJson.put("meta", new BasicDBObject(meta)); } String collectionName = MongoDBCouchbaseReplicator.defaultCollection; if (o.get(MongoDBCouchbaseReplicator.collectionField) != null) { collectionName = (String) o.get(MongoDBCouchbaseReplicator.collectionField); } try { if (MongoDBCouchbaseReplicator.replicationType.equalsIgnoreCase("insert_only")) { // this will raise an exception db.getCollection(collectionName).insert(mongoJson); } else { // insert & update db.getCollection(collectionName).save(mongoJson); } } catch (MongoException e) { if (e.getCode() == 11000) { logger.log(Level.INFO, "Not replicating updated document " + meta.get("id")); } else { logger.log(Level.SEVERE, e.getMessage()); } } } String id = (String) meta.get("id"); String rev = (String) meta.get("rev"); Map<String, Object> itemResponse = new HashMap<String, Object>(); itemResponse.put("id", id); itemResponse.put("rev", rev); result.add(itemResponse); } return result; }
From source file:com.grallandco.mongodb.beersample.BeerServlet.java
License:Open Source License
/** * Handle the /beers action.//w w w. j a v a 2 s .c o m * * @param request the HTTP request object. * @param response the HTTP response object. * @throws java.io.IOException * @throws javax.servlet.ServletException */ private void handleIndex(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { DBCursor cursor = db.getCollection("beer").find().sort(BasicDBObjectBuilder.start("name", 1).get()) .limit(20); ArrayList<HashMap<String, String>> beers = new ArrayList<HashMap<String, String>>(); while (cursor.hasNext()) { DBObject row = cursor.next(); HashMap<String, String> beer = new HashMap<String, String>(); beer.put("id", (String) row.get("_id")); beer.put("name", (String) row.get("name")); beer.put("brewery", (String) row.get("brewery_id")); beers.add(beer); } request.setAttribute("beers", beers); request.getRequestDispatcher("/WEB-INF/beers/index.jsp").forward(request, response); }
From source file:com.grallandco.mongodb.beersample.BeerServlet.java
License:Open Source License
/** * Handle the /beers/delete/<BEER-ID> action * * This method deletes a beer based on the given beer id. * * @param request the HTTP request object. * @param response the HTTP response object. * @throws java.io.IOException/*from w w w. jav a 2 s . com*/ * @throws javax.servlet.ServletException * @throws InterruptedException * @throws java.util.concurrent.ExecutionException */ private void handleDelete(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException, InterruptedException, ExecutionException { String beerId = request.getPathInfo().split("/")[2]; db.getCollection("beer").remove(BasicDBObjectBuilder.start("_id", beerId).get()); response.sendRedirect("/beers"); }
From source file:com.grallandco.mongodb.beersample.BeerServlet.java
License:Open Source License
/** * Handle the /beers/search action.// w ww .j a va2s. c o m * * @param request the HTTP request object. * @param response the HTTP response object. * @throws java.io.IOException * @throws javax.servlet.ServletException */ private void handleSearch(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { String name = request.getParameter("value").toLowerCase(); DBObject query = QueryBuilder.start().put("name") .regex(java.util.regex.Pattern.compile(name, Pattern.CASE_INSENSITIVE)).get(); DBCursor cursor = db.getCollection("beer").find(query).sort(BasicDBObjectBuilder.start("name", 1).get()) .limit(20); ArrayList<HashMap<String, String>> beers = new ArrayList<HashMap<String, String>>(); while (cursor.hasNext()) { DBObject row = cursor.next(); HashMap<String, String> beer = new HashMap<String, String>(); beer.put("id", (String) row.get("_id")); beer.put("name", (String) row.get("name")); beer.put("brewery", (String) row.get("brewery_id")); beers.add(beer); } response.setContentType("application/json"); PrintWriter out = response.getWriter(); out.print(gson.toJson(beers)); out.flush(); }
From source file:com.grallandco.mongodb.beersample.BreweryServlet.java
License:Open Source License
/** * Handle the /breweries action.// www.j av a 2s . co m * * @param request the HTTP request object. * @param response the HTTP response object. * @throws java.io.IOException * @throws javax.servlet.ServletException */ private void handleIndex(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { DBCursor cursor = db.getCollection("brewery").find().sort(BasicDBObjectBuilder.start("name", 1).get()) .limit(20); ArrayList<HashMap<String, String>> breweries = new ArrayList<HashMap<String, String>>(); while (cursor.hasNext()) { DBObject row = cursor.next(); HashMap<String, String> brewery = new HashMap<String, String>(); brewery.put("id", (String) row.get("_id")); brewery.put("name", (String) row.get("name")); breweries.add(brewery); } request.setAttribute("breweries", breweries); request.getRequestDispatcher("/WEB-INF/breweries/index.jsp").forward(request, response); }
From source file:com.grallandco.mongodb.beersample.BreweryServlet.java
License:Open Source License
/** * Handle the /breweries/delete/<BREWERY-ID> action * * This method deletes a brewery based on the given brewery id. * * @param request the HTTP request object. * @param response the HTTP response object. * @throws java.io.IOException/*from w w w.j a va2s .com*/ * @throws javax.servlet.ServletException * @throws InterruptedException * @throws java.util.concurrent.ExecutionException */ private void handleDelete(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException, InterruptedException, ExecutionException { String breweryId = request.getPathInfo().split("/")[2]; db.getCollection("brewery").remove(BasicDBObjectBuilder.start("_id", breweryId).get()); response.sendRedirect("/breweries"); }