List of usage examples for com.mongodb AggregationOptions builder
public static Builder builder()
From source file:kiaanfx.Kiaanfx.java
private static void getBuy() { try {// w ww . ja va 2 s . co m MongoClient mongoClient = new MongoClient("localhost", 27017); DB db = mongoClient.getDB("kiaan"); DBCollection coll = db.getCollection("buy"); //aggregate DBObject unwind = new BasicDBObject("$unwind", "$items"); //$group DBObject group_id = new BasicDBObject("_id", "$_id"); group_id.put("num", "$num"); group_id.put("person_id", "$person_id"); group_id.put("discount", "$discount"); group_id.put("increase", "$increase"); //$group -> $multiply BasicDBList args = new BasicDBList(); args.add("$items.value"); args.add("$items.price"); DBObject multiply = new BasicDBObject("$multiply", args); //$group -> $sum // DBObject group_sum = new BasicDBObject("$sum", multiply); DBObject group_field = new BasicDBObject(); group_field.put("_id", group_id); group_field.put("total", new BasicDBObject("$sum", multiply)); DBObject group = new BasicDBObject("$group", group_field); //$project DBObject project_field = new BasicDBObject("_id", "$_id._id"); project_field.put("person_id", "$_id.person_id"); project_field.put("num", "$_id.num"); BasicDBList arr = new BasicDBList(); arr.add("$total"); arr.add("$_id.discount"); arr.add("$_id.increase"); DBObject field_add = new BasicDBObject("$add", arr); project_field.put("sum", field_add); DBObject project = new BasicDBObject("$project", project_field); DBObject sort = new BasicDBObject("$sort", new BasicDBObject("_id", 1)); List<DBObject> pipeline = Arrays.asList(unwind, group, project, sort); // AggregationOutput output = coll.aggregate(pipeline); // for (DBObject result : output.results()) { // System.out.println(result); // } AggregationOptions aggregationOptions = AggregationOptions.builder().batchSize(100) .outputMode(AggregationOptions.OutputMode.CURSOR).allowDiskUse(true).build(); BasicDBObject dbo = new BasicDBObject(); BasicDBList dbl = new BasicDBList(); Cursor cursor = coll.aggregate(pipeline, aggregationOptions); // DBCollection person_col = db.getCollection("persons"); // BasicDBObject query = new BasicDBObject("items.personId",1); BasicDBObject fields = new BasicDBObject("items.$", 1).append("_id", false); // BasicDBList l_per = (BasicDBList) person_col.findOne(query, fields).get("items"); // BasicDBObject[] lightArr = l_per.toArray(new BasicDBObject[0]); // System.out.println(lightArr[0].get("_id")); // System.out.println(lightArr[0].get("first_name")); // BasicDBList result = new BasicDBList(); while (cursor.hasNext()) { dbo = (BasicDBObject) cursor.next(); // System.out.println(dbo.toString()); DBObject query = new BasicDBObject("items._id", (ObjectId) dbo.get("person_id")); BasicDBList lst_person = (BasicDBList) person_col.findOne(query, fields).get("items"); BasicDBObject[] lightArr = lst_person.toArray(new BasicDBObject[0]); // System.out.println(lightArr[0].get("first_name")); Date date = ((ObjectId) lightArr[0].get("_id")).getDate(); Calendar calendar = Calendar.getInstance(); calendar.setTime(date); persianCalendar persianCalendar = new persianCalendar(calendar); dbo.put("date", persianCalendar.getNumericDateFormatWithTime()); dbo.put("personId", lightArr[0].get("personId").toString()); dbo.put("first_name", lightArr[0].get("first_name").toString()); dbo.put("last_name", lightArr[0].get("last_name").toString()); data.add(new Person(dbo.get("num").toString(), dbo.get("date").toString(), dbo.get("personId").toString(), dbo.get("first_name").toString(), dbo.get("last_name").toString())); // buy_data.add(new buys(dbo.get("num").toString(), // dbo.get("date").toString(), // dbo.get("personId").toString(), // dbo.get("first_name").toString(), // dbo.get("last_name").toString(), // dbo.get("sum").toString() // )); dbo.remove("person_id"); // result.add(dbo); // System.out.println(dbo.get("first_name")); } System.out.println(dbo.toString()); } catch (Exception e) { System.err.println(e.getClass().getName() + ": " + e.getMessage()); } }
From source file:org.apache.calcite.adapter.mongodb.MongoTable.java
License:Apache License
/** Executes an "aggregate" operation on the underlying collection. * * <p>For example://from w ww. j a v a 2 s . c om * <code>zipsTable.aggregate( * "{$filter: {state: 'OR'}", * "{$group: {_id: '$city', c: {$sum: 1}, p: {$sum: '$pop'}}}") * </code></p> * * @param mongoDb MongoDB connection * @param fields List of fields to project; or null to return map * @param operations One or more JSON strings * @return Enumerator of results */ public Enumerable<Object> aggregate(final DB mongoDb, final List<Map.Entry<String, Class>> fields, final List<String> operations) { final List<DBObject> list = new ArrayList<DBObject>(); final BasicDBList versionArray = (BasicDBList) mongoDb.command("buildInfo").get("versionArray"); final Integer versionMajor = parseIntString(versionArray.get(0).toString()); final Integer versionMinor = parseIntString(versionArray.get(1).toString()); // final Integer versionMaintenance = parseIntString(versionArray // .get(2).toString()); // final Integer versionBuild = parseIntString(versionArray // .get(3).toString()); for (String operation : operations) { list.add((DBObject) JSON.parse(operation)); } final DBObject first = list.get(0); final List<DBObject> rest = Util.skip(list); final Function1<DBObject, Object> getter = MongoEnumerator.getter(fields); return new AbstractEnumerable<Object>() { public Enumerator<Object> enumerator() { final Iterator<DBObject> resultIterator; try { // Changed in version 2.6: The db.collection.aggregate() method // returns a cursor // and can return result sets of any size. // See: http://docs.mongodb.org/manual/core/aggregation-pipeline if (versionMajor > 1) { // MongoDB version 2.6+ if (versionMinor > 5) { AggregationOptions options = AggregationOptions.builder() .outputMode(AggregationOptions.OutputMode.CURSOR).build(); // Warning - this can result in a very large ArrayList! // but you should know your data and aggregate accordingly ArrayList<DBObject> resultAsArrayList = new ArrayList<DBObject>( Util.toList(mongoDb.getCollection(collectionName).aggregate(list, options))); resultIterator = resultAsArrayList.iterator(); } else { // Pre MongoDB version 2.6 AggregationOutput result = aggregateOldWay(mongoDb.getCollection(collectionName), first, rest); resultIterator = result.results().iterator(); } } else { // Pre MongoDB version 2 AggregationOutput result = aggregateOldWay(mongoDb.getCollection(collectionName), first, rest); resultIterator = result.results().iterator(); } } catch (Exception e) { throw new RuntimeException( "While running MongoDB query " + Util.toString(operations, "[", ",\n", "]"), e); } return new MongoEnumerator(resultIterator, getter); } }; }
From source file:org.fastmongo.odm.repository.MongoTemplate.java
License:Apache License
private AggregationOptions aggregationOptions() { return AggregationOptions.builder().maxTime(maxTimeMS, TimeUnit.MILLISECONDS).build(); }
From source file:org.kiaan.Main.java
public static void getBranchesWithProject() { try {//from w ww .j a va2s. c o m MongoClient mongoClient = new MongoClient("localhost", 27017); DB db = mongoClient.getDB("kiaan"); DBCollection coll = db.getCollection("banks"); // BasicDBObject doc = new BasicDBObject("_id" , false).append("name", true); // BasicDBObject doc1 = new BasicDBObject("name" , ""); // BasicDBObject doc = new BasicDBObject("_id" , false).append("branches.name", true); // DBCursor cursor = coll.find(null, doc); DBObject unwind = new BasicDBObject("$unwind", "$branches"); DBObject field = new BasicDBObject("_id", false); field.put("name", "$name"); field.put("branch_id", "$branches.branch_id"); field.put("branch_name", "$branches.name"); DBObject project = new BasicDBObject("$project", field); DBObject sort = new BasicDBObject("$sort", new BasicDBObject("name", 1)); List<DBObject> pipeline = Arrays.asList(unwind, project, sort); // AggregationOutput output = coll.aggregate(pipeline); // for (DBObject result : output.results()) { // System.out.println(result); // } AggregationOptions aggregationOptions = AggregationOptions.builder().batchSize(100) .outputMode(AggregationOptions.OutputMode.CURSOR).allowDiskUse(true).build(); BasicDBObject dbo = new BasicDBObject(); BasicDBList dbl = new BasicDBList(); Cursor cursor = coll.aggregate(pipeline, aggregationOptions); while (cursor.hasNext()) { dbo = (BasicDBObject) cursor.next(); System.out.println(dbo.toString()); // dbl.add(cursor.next()); } System.out.println(dbl.toString()); // while(cursor.hasNext()) { // DBObject obj = cursor.next(); // System.out.println(obj.get("branches")); //// Double first = (Double)obj.get("id"); //// String last = (String)obj.get("name"); //// ObjectId id = (ObjectId)obj.get("_id"); //// model.addRow(new Object[] { id, first, last }); // } // tbl.setModel(model); } catch (Exception e) { System.err.println(e.getClass().getName() + ": " + e.getMessage()); } }
From source file:org.kiaan.Main.java
private static void getBuy() { try {//from w w w. j a va2 s .co m MongoClient mongoClient = new MongoClient("localhost", 27017); DB db = mongoClient.getDB("kiaan"); DBCollection coll = db.getCollection("buy"); //aggregate DBObject unwind = new BasicDBObject("$unwind", "$items"); //$group DBObject group_id = new BasicDBObject("_id", "$_id"); group_id.put("num", "$num"); group_id.put("person_id", "$person_id"); group_id.put("discount", "$discount"); group_id.put("increase", "$increase"); //$group -> $multiply BasicDBList args = new BasicDBList(); args.add("$items.value"); args.add("$items.price"); DBObject multiply = new BasicDBObject("$multiply", args); //$group -> $sum // DBObject group_sum = new BasicDBObject("$sum", multiply); DBObject group_field = new BasicDBObject(); group_field.put("_id", group_id); group_field.put("total", new BasicDBObject("$sum", multiply)); DBObject group = new BasicDBObject("$group", group_field); //$project DBObject project_field = new BasicDBObject("_id", "$_id._id"); project_field.put("person_id", "$_id.person_id"); project_field.put("num", "$_id.num"); BasicDBList arr = new BasicDBList(); arr.add("$total"); arr.add("$_id.discount"); arr.add("$_id.increase"); DBObject field_add = new BasicDBObject("$add", arr); project_field.put("sum", field_add); DBObject project = new BasicDBObject("$project", project_field); DBObject sort = new BasicDBObject("$sort", new BasicDBObject("_id", 1)); List<DBObject> pipeline = Arrays.asList(unwind, group, project, sort); // AggregationOutput output = coll.aggregate(pipeline); // for (DBObject result : output.results()) { // System.out.println(result); // } AggregationOptions aggregationOptions = AggregationOptions.builder().batchSize(100) .outputMode(AggregationOptions.OutputMode.CURSOR).allowDiskUse(true).build(); BasicDBObject dbo = new BasicDBObject(); BasicDBList dbl = new BasicDBList(); Cursor cursor = coll.aggregate(pipeline, aggregationOptions); // DBCollection person_col = db.getCollection("persons"); // BasicDBObject query = new BasicDBObject("items.personId",1); BasicDBObject fields = new BasicDBObject("items.$", 1).append("_id", false); // BasicDBList l_per = (BasicDBList) person_col.findOne(query, fields).get("items"); // BasicDBObject[] lightArr = l_per.toArray(new BasicDBObject[0]); // System.out.println(lightArr[0].get("_id")); // System.out.println(lightArr[0].get("first_name")); BasicDBList result = new BasicDBList(); while (cursor.hasNext()) { dbo = (BasicDBObject) cursor.next(); // System.out.println(dbo.toString()); DBObject query = new BasicDBObject("items._id", (ObjectId) dbo.get("person_id")); BasicDBList lst_person = (BasicDBList) person_col.findOne(query, fields).get("items"); BasicDBObject[] lightArr = lst_person.toArray(new BasicDBObject[0]); // System.out.println(lightArr[0].get("first_name")); Date date = ((ObjectId) lightArr[0].get("_id")).getDate(); Calendar calendar = Calendar.getInstance(); calendar.setTime(date); persianCalendar persianCalendar = new persianCalendar(calendar); dbo.put("date", persianCalendar.getNumericDateFormatWithTime()); dbo.put("personId", lightArr[0].get("personId").toString()); dbo.put("first_name", lightArr[0].get("first_name").toString()); dbo.put("last_name", lightArr[0].get("last_name").toString()); dbo.removeField("person_id"); result.add(dbo); // System.out.println(dbo.get("num")); } System.out.println(result.toString()); } catch (Exception e) { System.err.println(e.getClass().getName() + ": " + e.getMessage()); } }
From source file:org.knowrob.knowrob_robcog.MongoRobcogQueries.java
License:Open Source License
public double[] GetActorPoseAt(String actorName, double timestamp) { // $and list for querying the $match in the aggregation BasicDBList time_and_name = new BasicDBList(); // add the timestamp and the actor name time_and_name.add(new BasicDBObject("timestamp", new BasicDBObject("$lte", timestamp))); time_and_name.add(new BasicDBObject("actors.name", actorName)); // create the pipeline operations, first the $match DBObject match_time_and_name = new BasicDBObject("$match", new BasicDBObject("$and", time_and_name)); // sort the results in descending order on the timestamp (keep most recent result first) DBObject sort_desc = new BasicDBObject("$sort", new BasicDBObject("timestamp", -1)); // $limit the result to 1, we only need one pose DBObject limit_result = new BasicDBObject("$limit", 1); // $unwind actors in order to output only the queried actor DBObject unwind_actors = new BasicDBObject("$unwind", "$actors"); // $match for the given actor name from the unwinded actors DBObject match_actor = new BasicDBObject("$match", new BasicDBObject("actors.name", actorName)); // build the $projection operation DBObject proj_fields = new BasicDBObject("_id", 0); proj_fields.put("timestamp", 1); proj_fields.put("pos", "$actors.pos"); proj_fields.put("rot", "$actors.rot"); DBObject project = new BasicDBObject("$project", proj_fields); // run aggregation List<DBObject> pipeline = Arrays.asList(match_time_and_name, sort_desc, limit_result, unwind_actors, match_actor, project);//from w ww . ja va 2 s . c om AggregationOptions aggregationOptions = AggregationOptions.builder().batchSize(100) .outputMode(AggregationOptions.OutputMode.CURSOR).allowDiskUse(true).build(); // get results Cursor cursor = this.MongoRobcogConn.coll.aggregate(pipeline, aggregationOptions); // if query has a response, return the pose if (cursor.hasNext()) { // get the first document as the next cursor and append the metadata to it BasicDBObject first_doc = (BasicDBObject) cursor.next(); // close cursor cursor.close(); // get the pose return new double[] { ((BasicDBObject) first_doc.get("pos")).getDouble("x"), ((BasicDBObject) first_doc.get("pos")).getDouble("y"), ((BasicDBObject) first_doc.get("pos")).getDouble("z"), ((BasicDBObject) first_doc.get("rot")).getDouble("w"), ((BasicDBObject) first_doc.get("rot")).getDouble("x"), ((BasicDBObject) first_doc.get("rot")).getDouble("y"), ((BasicDBObject) first_doc.get("rot")).getDouble("z") }; } else { System.out.println("Java - GetActorPose - No results found, returning empty list.."); return new double[0]; } }
From source file:org.knowrob.knowrob_robcog.MongoRobcogQueries.java
License:Open Source License
/** * Query the Traj of the actor between the given timepoints */// w w w .j a va 2s .c om public double[][] GetActorTraj(String actorName, String start, String end, double deltaT) { // transform the knowrob time to double with 3 decimal precision final double start_ts = (double) Math.round(parseTime_d(start) * 1000) / 1000; final double end_ts = (double) Math.round(parseTime_d(end) * 1000) / 1000; // create the pipeline operations, first with the $match check the times DBObject match_time = new BasicDBObject("$match", new BasicDBObject("timestamp", new BasicDBObject("$gte", start_ts).append("$lte", end_ts))); // $unwind the actors DBObject unwind_actors = new BasicDBObject("$unwind", "$actors"); // $match for the given actor name from the unwinded actors DBObject match_actor = new BasicDBObject("$match", new BasicDBObject("actors.name", actorName)); // build the $projection operation DBObject proj_fields = new BasicDBObject("_id", 0); proj_fields.put("timestamp", 1); proj_fields.put("pos", "$actors.pos"); proj_fields.put("rot", "$actors.rot"); DBObject project = new BasicDBObject("$project", proj_fields); // run aggregation List<DBObject> pipeline = Arrays.asList(match_time, unwind_actors, match_actor, project); AggregationOptions aggregationOptions = AggregationOptions.builder().batchSize(100) .outputMode(AggregationOptions.OutputMode.CURSOR).allowDiskUse(true).build(); // get results Cursor cursor = this.MongoRobcogConn.coll.aggregate(pipeline, aggregationOptions); // Traj as dynamic array ArrayList<double[]> traj_list = new ArrayList<double[]>(); // if the query returned nothing, get the most recent pose if (!cursor.hasNext()) { System.out.println("Java - GetActorTraj - No results found, returning most recent pose.."); // get the most recent pose traj_list.add(this.GetActorPoseAt(actorName, start)); // cast from dynamic array to standard array return traj_list.toArray(new double[traj_list.size()][7]); } // timestamp used for deltaT double prev_ts = 0; // while query has a response, return the pose while (cursor.hasNext()) { // get the first document as the next cursor and append the metadata to it BasicDBObject curr_doc = (BasicDBObject) cursor.next(); // get the curr timestamp double curr_ts = curr_doc.getDouble("timestamp"); // if time diff > then deltaT add position to trajectory if (curr_ts - prev_ts > deltaT) { // get the current pose traj_list.add(new double[] { ((BasicDBObject) curr_doc.get("pos")).getDouble("x"), ((BasicDBObject) curr_doc.get("pos")).getDouble("y"), ((BasicDBObject) curr_doc.get("pos")).getDouble("z"), ((BasicDBObject) curr_doc.get("rot")).getDouble("w"), ((BasicDBObject) curr_doc.get("rot")).getDouble("x"), ((BasicDBObject) curr_doc.get("rot")).getDouble("y"), ((BasicDBObject) curr_doc.get("rot")).getDouble("z") }); prev_ts = curr_ts; //System.out.println(curr_doc.toString()); } } // close cursor cursor.close(); // cast from dynamic array to standard array return traj_list.toArray(new double[traj_list.size()][7]); }
From source file:org.knowrob.knowrob_robcog.MongoRobcogQueries.java
License:Open Source License
/** * Query the Pose of the actors bone at the given timepoint (or the most recent one) *//*from w w w . j a v a2 s . c o m*/ public double[] GetBonePoseAt(String actorName, String boneName, String timestampStr) { final double timestamp = (double) Math.round(parseTime_d(timestampStr) * 1000) / 1000; // $and list for querying the $match in the aggregation BasicDBList time_and_name = new BasicDBList(); // add the timestamp and the actor name time_and_name.add(new BasicDBObject("timestamp", new BasicDBObject("$lte", timestamp))); time_and_name.add(new BasicDBObject("actors.name", actorName)); // create the pipeline operations, first the $match DBObject match_time_and_name = new BasicDBObject("$match", new BasicDBObject("$and", time_and_name)); // sort the results in descending order on the timestamp (keep most recent result first) DBObject sort_desc = new BasicDBObject("$sort", new BasicDBObject("timestamp", -1)); // $limit the result to 1, we only need one pose DBObject limit_result = new BasicDBObject("$limit", 1); // $unwind actors in order to output only the queried actor DBObject unwind_actors = new BasicDBObject("$unwind", "$actors"); // $match for the given actor name from the unwinded actors DBObject match_actor = new BasicDBObject("$match", new BasicDBObject("actors.name", actorName)); // build the $projection operation DBObject proj_bone_fields = new BasicDBObject("_id", 0); proj_bone_fields.put("timestamp", 1); proj_bone_fields.put("actors.bones", 1); DBObject project_bones = new BasicDBObject("$project", proj_bone_fields); // $unwind the bones DBObject unwind_bones = new BasicDBObject("$unwind", "$actors.bones"); // $match for the given bone name from the unwinded bones DBObject match_bone = new BasicDBObject("$match", new BasicDBObject("actors.bones.name", boneName)); // build the final $projection operation DBObject proj_fields = new BasicDBObject("timestamp", 1); proj_fields.put("pos", "$actors.bones.pos"); proj_fields.put("rot", "$actors.bones.rot"); DBObject project = new BasicDBObject("$project", proj_fields); // run aggregation List<DBObject> pipeline = Arrays.asList(match_time_and_name, sort_desc, limit_result, unwind_actors, match_actor, project_bones, unwind_bones, match_bone, project); AggregationOptions aggregationOptions = AggregationOptions.builder().batchSize(100) .outputMode(AggregationOptions.OutputMode.CURSOR).allowDiskUse(true).build(); // get results Cursor cursor = this.MongoRobcogConn.coll.aggregate(pipeline, aggregationOptions); // if query has a response, return the pose if (cursor.hasNext()) { // get the first document as the next cursor and append the metadata to it BasicDBObject first_doc = (BasicDBObject) cursor.next(); // close cursor since we only care about one value cursor.close(); // get the pose return new double[] { ((BasicDBObject) first_doc.get("pos")).getDouble("x"), ((BasicDBObject) first_doc.get("pos")).getDouble("y"), ((BasicDBObject) first_doc.get("pos")).getDouble("z"), ((BasicDBObject) first_doc.get("rot")).getDouble("w"), ((BasicDBObject) first_doc.get("rot")).getDouble("x"), ((BasicDBObject) first_doc.get("rot")).getDouble("y"), ((BasicDBObject) first_doc.get("rot")).getDouble("z") }; } else { System.out.println("Java - GetBonePose - No results found, returning empty list.."); return new double[0]; } }
From source file:org.knowrob.knowrob_robcog.MongoRobcogQueries.java
License:Open Source License
/** * Query the Traj of the actors bone between the given timepoints */// ww w. j av a 2 s .co m public double[][] GetBoneTraj(String actorName, String boneName, String start, String end, double deltaT) { // transform the knowrob time to double with 3 decimal precision final double start_ts = (double) Math.round(parseTime_d(start) * 1000) / 1000; final double end_ts = (double) Math.round(parseTime_d(end) * 1000) / 1000; // create the pipeline operations, first with the $match check the times DBObject match_time = new BasicDBObject("$match", new BasicDBObject("timestamp", new BasicDBObject("$gte", start_ts).append("$lte", end_ts))); // $unwind the actors DBObject unwind_actors = new BasicDBObject("$unwind", "$actors"); // $match for the given actor name from the unwinded actors DBObject match_actor = new BasicDBObject("$match", new BasicDBObject("actors.name", actorName)); // build the $projection operation DBObject proj_fields = new BasicDBObject("_id", 0); proj_fields.put("timestamp", 1); proj_fields.put("pos", "$actors.pos"); proj_fields.put("rot", "$actors.rot"); DBObject project = new BasicDBObject("$project", proj_fields); // run aggregation List<DBObject> pipeline = Arrays.asList(match_time, unwind_actors, match_actor, project); AggregationOptions aggregationOptions = AggregationOptions.builder().batchSize(100) .outputMode(AggregationOptions.OutputMode.CURSOR).allowDiskUse(true).build(); // get results Cursor cursor = this.MongoRobcogConn.coll.aggregate(pipeline, aggregationOptions); // Traj as dynamic array ArrayList<double[]> traj_list = new ArrayList<double[]>(); // if the query returned nothing, get the most recent pose if (!cursor.hasNext()) { System.out.println("Java - GetBoneTraj - No results found, returning most recent pose.."); // get the most recent pose traj_list.add(this.GetBonePoseAt(actorName, boneName, start)); // cast from dynamic array to standard array return traj_list.toArray(new double[traj_list.size()][7]); } // timestamp used for deltaT double prev_ts = 0; // if query has a response, return the pose while (cursor.hasNext()) { // get the first document as the next cursor and append the metadata to it BasicDBObject curr_doc = (BasicDBObject) cursor.next(); // get the curr timestamp double curr_ts = curr_doc.getDouble("timestamp"); // if time diff > then deltaT add position to trajectory if (curr_ts - prev_ts > deltaT) { // get the current pose traj_list.add(new double[] { ((BasicDBObject) curr_doc.get("pos")).getDouble("x"), ((BasicDBObject) curr_doc.get("pos")).getDouble("y"), ((BasicDBObject) curr_doc.get("pos")).getDouble("z"), ((BasicDBObject) curr_doc.get("rot")).getDouble("w"), ((BasicDBObject) curr_doc.get("rot")).getDouble("x"), ((BasicDBObject) curr_doc.get("rot")).getDouble("y"), ((BasicDBObject) curr_doc.get("rot")).getDouble("z") }); prev_ts = curr_ts; //System.out.println(curr_doc.toString()); } } // close cursor cursor.close(); // cast from dynamic array to standard array return traj_list.toArray(new double[traj_list.size()][7]); }
From source file:org.knowrob.knowrob_robcog.MongoRobcogQueries.java
License:Open Source License
/** * Query the Names of the actor bones/*from www . j a va 2s .c o m*/ */ public String[] GetBonesNames(String actorName) { // create the pipeline operations, first the $match DBObject match_name = new BasicDBObject("$match", new BasicDBObject("actors.name", actorName)); // $limit the result to 1 DBObject limit_result = new BasicDBObject("$limit", 1); // $unwind actors in order to output only the queried actor DBObject unwind_actors = new BasicDBObject("$unwind", "$actors"); // $match for the given actor name from the unwinded actors DBObject match_actor = new BasicDBObject("$match", new BasicDBObject("actors.name", actorName)); // build the $projection operation DBObject proj_fields = new BasicDBObject("_id", 0); proj_fields.put("bones_names", "$actors.bones.name"); DBObject project = new BasicDBObject("$project", proj_fields); // run aggregation List<DBObject> pipeline = Arrays.asList(match_name, limit_result, unwind_actors, match_actor, project); AggregationOptions aggregationOptions = AggregationOptions.builder().batchSize(100) .outputMode(AggregationOptions.OutputMode.CURSOR).allowDiskUse(true).build(); // get results Cursor cursor = this.MongoRobcogConn.coll.aggregate(pipeline, aggregationOptions); // if query has a response, return the names if (cursor.hasNext()) { // get the first document as the next cursor and append the metadata to it BasicDBObject first_doc = (BasicDBObject) cursor.next(); // close cursor since we only care about one value cursor.close(); // get the bone names as list BasicDBList names = (BasicDBList) first_doc.get("bones_names"); // return as array of string return names.toArray(new String[names.size()]); } else // else return empty list { System.out.println("Java - GetBonesNames - No results found, returning empty list.."); return new String[0]; } }