List of usage examples for com.mongodb AggregationOutput results
List results
To view the source code for com.mongodb AggregationOutput results.
Click Source Link
From source file:org.eclipse.birt.data.oda.mongodb.internal.impl.MDbOperation.java
License:Open Source License
static Iterable<DBObject> callAggregateCmd(DBCollection dbCollection, QueryProperties queryProps) throws OdaException { if (!queryProps.hasAggregateCommand()) return null; DBObject operationExprObj = queryProps.getOperationExprAsParsedObject(true); if (operationExprObj == null) return null; // convert user-specified operation expression to operation pipeline DBObject firstOp = QueryProperties.getFirstObjectSet(operationExprObj); if (firstOp == null) return null; // no valid DBObject operation DBObject[] addlOps = QueryProperties.getSecondaryObjectSets(operationExprObj); // aggregation $limit and $skip operators applies to the number // of documents in the *input* pipeline, and thus cannot be used to apply // the searchLimit and numSkipDocuments properties defined for data set // $match and $sort pipeline operators are built in an aggregate command // execute the aggregate command AggregationOutput output; try {//from www.jav a 2 s . co m output = addlOps != null ? dbCollection.aggregate(firstOp, addlOps) : dbCollection.aggregate(firstOp); output.getCommandResult().throwOnError(); return output.results(); } catch (RuntimeException ex) { OdaException odaEx = new OdaException(Messages.mDbOp_aggrCmdFailed); odaEx.initCause(ex); throw odaEx; } }
From source file:org.envirocar.server.mongo.dao.MongoMeasurementDao.java
License:Open Source License
List<Key<MongoTrack>> getTrackKeysByBbox(MeasurementFilter filter) { ArrayList<DBObject> filters = new ArrayList<DBObject>(4); if (filter.hasGeometry()) { filters.add(matchGeometry(filter.getGeometry())); }//w w w . j ava 2s.c om if (filter.hasUser()) { filters.add(matchUser(filter.getUser())); } if (filter.hasTrack()) { filters.add(matchTrack(filter.getTrack())); } if (filter.hasTemporalFilter()) { filters.add(matchTime(filter.getTemporalFilter())); } final AggregationOutput out; if (filters.isEmpty()) { out = aggregate(project(), group()); } else { int size = filters.size(); if (size == 1) { out = aggregate(filters.get(0), project(), group()); } else { DBObject first = filters.get(0); DBObject[] other = new DBObject[size + 1]; for (int i = 1; i < size; ++i) { other[i - 1] = filters.get(i); } other[other.length - 2] = project(); other[other.length - 1] = group(); out = aggregate(first, other); } } return toKeyList(out.results()); }
From source file:org.envirocar.server.mongo.dao.MongoStatisticsDao.java
License:Open Source License
private MongoStatistics getStatistics1(StatisticsFilter request) { MongoStatisticKey key = key(request); MongoStatistics v = this.dao.get(key); if (v == null) { AggregationOutput aggregate = aggregate(matches(request), project(), unwind(), group()); List<MongoStatistic> statistics = parseStatistics(aggregate.results()); v = new MongoStatistics(key, statistics); this.dao.save(v); }//from w w w . j av a 2 s.co m return v; }
From source file:org.exist.mongodb.xquery.mongodb.collection.Aggregate.java
License:Open Source License
@Override public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException { try {//from w w w .ja va2 s . com // Verify clientid and get client String mongodbClientId = args[0].itemAt(0).getStringValue(); MongodbClientStore.getInstance().validate(mongodbClientId); MongoClient client = MongodbClientStore.getInstance().get(mongodbClientId); // Get parameters String dbname = args[1].itemAt(0).getStringValue(); String collection = args[2].itemAt(0).getStringValue(); List<DBObject> pipeline = convertPipeline(args[3]); // Get collection in database DB db = client.getDB(dbname); DBCollection dbcol = db.getCollection(collection); // Execute query AggregationOutput aggrOutput = dbcol.aggregate(pipeline); // Parse results Sequence retVal = new ValueSequence(); for (DBObject result : aggrOutput.results()) { retVal.add(new StringValue(result.toString())); } return retVal; } catch (JSONParseException ex) { LOG.error(ex.getMessage()); throw new XPathException(this, MongodbModule.MONG0004, ex.getMessage()); } catch (XPathException ex) { LOG.error(ex.getMessage(), ex); throw new XPathException(this, ex.getMessage(), ex); } catch (MongoException ex) { LOG.error(ex.getMessage(), ex); throw new XPathException(this, MongodbModule.MONG0002, ex.getMessage()); } catch (Throwable ex) { LOG.error(ex.getMessage(), ex); throw new XPathException(this, MongodbModule.MONG0003, ex.getMessage()); } }
From source file:org.forgerock.openidm.repo.mongodb.impl.query.Queries.java
License:Open Source License
protected List<DBObject> executeQuery(QueryInfo queryInfo, Map<String, Object> params, DBCollection collection) throws BadRequestException { List<DBObject> result = null; if (queryInfo.isGroupQuery()) { String resultKey = ""; List<String> list = queryInfo.getAggregationParams(); List<DBObject> dboList = new ArrayList<DBObject>(); DBObject firstParam = new BasicDBObject(); boolean first = true; for (String s : list) { DBObject query = resolveQuery(s, params); if (first) { firstParam = query;/* w w w. j a va 2 s .co m*/ first = !first; } else { dboList.add(query); } } AggregationOutput output = collection.aggregate(firstParam, (DBObject[]) dboList.toArray(new BasicDBObject[0])); if (output.results().iterator().hasNext()) { result = new ArrayList<DBObject>(); } for (DBObject obj : output.results()) { result.add(obj); } } else { String q = (queryInfo.getQuery() == null) ? "{}" : queryInfo.getQuery(); DBObject query = resolveQuery(q, params); String f = (queryInfo.getFileds() == null) ? "{}" : queryInfo.getFileds(); DBObject fields = resolveQuery(f, params); String s = (queryInfo.getSort() == null) ? "{}" : queryInfo.getSort(); DBObject sort = resolveQuery(s, params); DBCursor cur = null; try { cur = collection.find(query, fields).sort(sort); result = cur.toArray(); } catch (Exception ex) { throw new BadRequestException(ex.getMessage()); } finally { cur.close(); } } return result; }
From source file:org.hbrs.nosql.mongoweb.gui.views.UseCase6.java
@Override protected void setUp() { //Add your Vaadin Coding here ;) //MongoDB Connector MongoDBConnector mongo = new MongoDBConnector(); VerticalLayout uc6 = new VerticalLayout(); Button button = new Button("Welcher Professor hat die meisten Arbeiten untersttzt?"); button.addClickListener(e -> {//from w w w . j a v a2 s. com uc6.removeAllComponents(); LocalTime t1 = LocalTime.now(); // load list with values AggregationOutput uc6answer = mongo.getQuery6(); LocalTime t2 = LocalTime.now(); Duration elapsed = Duration.between(t1, t2); // query over list for (DBObject place : uc6answer.results()) { if (!place.get("_id").equals("")) { uc6.addComponent(new Label(place.get("_id") + ": " + place.get("Anzahl"))); } } uc6.addComponent(new Label("Time: " + elapsed)); addComponent(uc6); }); addComponents(button); setMargin(true); setSpacing(true); }
From source file:org.hbrs.nosql.mongoweb.gui.views.UseCase8.java
@Override protected void setUp() { //Add your Vaadin Coding here ;) //MongoDB Connector MongoDBConnector mongo = new MongoDBConnector(); VerticalLayout uc8 = new VerticalLayout(); Label uc8label = new Label("Welche Professoren haben wieviele Arbeiten zu einem Thema betreut?"); TextField uc8input = new TextField(); uc8input.setCaption("Thema:"); Button button = new Button("Suche"); button.addClickListener(e -> {//from w ww. j a v a 2 s.c o m uc8.removeAllComponents(); LocalTime t1 = LocalTime.now(); // load list with values AggregationOutput uc8answer = mongo.getQuery8(uc8input.getValue()); LocalTime t2 = LocalTime.now(); Duration elapsed = Duration.between(t1, t2); // query over list for (DBObject place : uc8answer.results()) { if (!place.get("_id").equals("")) { uc8.addComponent(new Label(place.get("_id") + ": " + place.get("Anzahl"))); } } uc8.addComponent(new Label("Time: " + elapsed)); addComponent(uc8); }); addComponents(uc8label, uc8input, button); setMargin(true); setSpacing(true); }
From source file:org.mandar.analysis.recsys2014.models.NeoMongoDBDataModel.java
License:Apache License
private void buildModel() throws UnknownHostException { userIsObject = false;//from w w w. ja v a 2 s.c om itemIsObject = false; idCounter = 0; preferenceIsString = true; MongoClient mongoDDBB = new MongoClient(mongoHost, mongoPort); DB db = mongoDDBB.getDB(mongoDB); mongoTimestamp = new Date(0); FastByIDMap<PreferenceArray> userIDPrefMap = new FastByIDMap<PreferenceArray>(); if (!mongoAuth || db.authenticate(mongoUsername, mongoPassword.toCharArray())) { collection = db.getCollection(mongoCollection); collectionMap = db.getCollection(mongoMapCollection); DBObject indexObj = new BasicDBObject(); indexObj.put("element_id", 1); collectionMap.createIndex(indexObj); indexObj = new BasicDBObject(); indexObj.put("long_value", 1); // Now the $group operation DBObject groupFields = new BasicDBObject("_id", "$" + mongoUserID); groupFields.put("count", new BasicDBObject("$sum", 1)); DBObject group = new BasicDBObject("$group", groupFields); // Finally the $sort operation DBObject sort = new BasicDBObject("$sort", new BasicDBObject("count", -1)); log.debug("Running Aggregation query"); // run aggregation List<DBObject> pipeline = Arrays.asList(group, sort); AggregationOutput output = collection.aggregate(pipeline); //Map<Long,Long> prefArrSize = new HashMap<Long, Long>(); Long2IntMap prefArrSize = new Long2IntArrayMap(); Long2IntMap prefArrCount = new Long2IntArrayMap(); for (DBObject result : output.results()) { prefArrSize.put(Long.parseLong(result.get("_id").toString()), Integer.parseInt(result.get("count").toString())); prefArrCount.put(Long.parseLong(result.get("_id").toString()), 0); log.debug(result.toString()); } log.debug("Aggregation query, Long2LongMap created"); collectionMap.createIndex(indexObj); collectionMap.remove(new BasicDBObject()); DBCursor cursor = collection.find(); int count; while (cursor.hasNext()) { Map<String, Object> user = (Map<String, Object>) cursor.next().toMap(); if (!user.containsKey("deleted_at")) { long userID = Long.parseLong(fromIdToLong(getID(user.get(mongoUserID), true), true)); long itemID = Long.parseLong(fromIdToLong(getID(user.get(mongoItemID), false), false)); float ratingValue = getPreference(user.get(mongoPreference)); PreferenceArray userPrefs = userIDPrefMap.get(userID); //log.info("user {} items rated {}", user.get(mongoUserID), prefArrSize.get(user.get(mongoUserID))); if (userPrefs == null) { userPrefs = new GenericUserPreferenceArray(prefArrSize.get(user.get(mongoUserID))); userIDPrefMap.put(userID, userPrefs); } count = prefArrCount.get(user.get(mongoUserID)); userPrefs.set(count, new GenericPreference(userID, itemID, ratingValue)); count++; prefArrCount.put(Long.parseLong(user.get(mongoUserID).toString()), count); if (user.containsKey("created_at") && mongoTimestamp.compareTo(getDate(user.get("created_at"))) < 0) { mongoTimestamp = getDate(user.get("created_at")); } } } } delegate = new GenericDataModel(userIDPrefMap); }
From source file:org.mongodb.workshop.api.ComicsService.java
License:Apache License
@GET @Path("groupBy/price") @ApiOperation(value = "Get the number of comics per price") public DBObject[] aggregateByPrice() { DBObject step1 = new BasicDBObject("$unwind", "$prices"); DBObject step2 = BasicDBObjectBuilder.start().push("$match").add("prices.type", "printPrice") .push("prices.price").add("$gt", 0).get(); DBObject step3 = BasicDBObjectBuilder.start().push("$group").add("_id", "$prices.price").push("total") .append("$sum", 1).get(); DBObject step4 = BasicDBObjectBuilder.start().push("$sort").add("_id", 1).get(); List<DBObject> pipeline = Arrays.<DBObject>asList(step1, step2, step3, step4); AggregationOutput output = comicsCollection.aggregate(pipeline); List<DBObject> items = new ArrayList<DBObject>(); for (DBObject item : output.results()) { items.add(item);/*from ww w . ja v a2 s .co m*/ } return items.toArray(new DBObject[items.size()]); }
From source file:org.pentaho.di.trans.steps.mongodbinput.MongoDbInput.java
License:Open Source License
protected void initQuery() throws KettleException, MongoDbException { // close any previous cursor if (data.cursor != null) { data.cursor.close();//from w ww .ja v a 2 s .c o m } // check logging level and only set to false if // logging level at least detailed if (log.isDetailed()) { m_serverDetermined = false; } String query = environmentSubstitute(meta.getJsonQuery()); String fields = environmentSubstitute(meta.getFieldsName()); if (Const.isEmpty(query) && Const.isEmpty(fields)) { if (meta.getQueryIsPipeline()) { throw new KettleException(BaseMessages.getString(MongoDbInputMeta.PKG, "MongoDbInput.ErrorMessage.EmptyAggregationPipeline")); //$NON-NLS-1$ } data.cursor = data.collection.find(); } else { if (meta.getQueryIsPipeline()) { if (Const.isEmpty(query)) { throw new KettleException(BaseMessages.getString(MongoDbInputMeta.PKG, "MongoDbInput.ErrorMessage.EmptyAggregationPipeline")); //$NON-NLS-1$ } if (meta.getExecuteForEachIncomingRow() && m_currentInputRowDrivingQuery != null) { // do field value substitution query = fieldSubstitute(query, getInputRowMeta(), m_currentInputRowDrivingQuery); } logDetailed(BaseMessages.getString(PKG, "MongoDbInput.Message.QueryPulledDataFrom", query)); List<DBObject> pipeline = MongodbInputDiscoverFieldsImpl.jsonPipelineToDBObjectList(query); DBObject firstP = pipeline.get(0); DBObject[] remainder = null; if (pipeline.size() > 1) { remainder = new DBObject[pipeline.size() - 1]; for (int i = 1; i < pipeline.size(); i++) { remainder[i - 1] = pipeline.get(i); } } else { remainder = new DBObject[0]; } AggregationOutput result = data.collection.aggregate(firstP, remainder); data.m_pipelineResult = result.results().iterator(); if (first) { // log the server used for the first query at the basic level logBasic(BaseMessages.getString(PKG, "MongoDbInput.Message.AggregationPulledDataFrom", result //$NON-NLS-1$ .getServerUsed().toString())); } else { // only log the server used to pull for each subsequent row at the // detailed level logDetailed(BaseMessages.getString(PKG, "MongoDbInput.Message.AggregationPulledDataFrom", result //$NON-NLS-1$ .getServerUsed().toString())); } } else { if (meta.getExecuteForEachIncomingRow() && m_currentInputRowDrivingQuery != null) { // do field value substitution query = fieldSubstitute(query, getInputRowMeta(), m_currentInputRowDrivingQuery); fields = fieldSubstitute(fields, getInputRowMeta(), m_currentInputRowDrivingQuery); } logDetailed(BaseMessages.getString(PKG, "MongoDbInput.Message.ExecutingQuery", query)); DBObject dbObject = (DBObject) JSON.parse(Const.isEmpty(query) ? "{}" //$NON-NLS-1$ : query); DBObject dbObject2 = (DBObject) JSON.parse(fields); data.cursor = data.collection.find(dbObject, dbObject2); } } }