List of usage examples for com.mongodb DBCollection insert
public WriteResult insert(final List<? extends DBObject> documents)
From source file:org.jeo.mongo.GeoJSONTestData.java
License:Open Source License
public void setUp(DBCollection dbcol, MongoWorkspace workspace) throws IOException { VectorDataset data = TestData.states(); for (Feature f : data.cursor(new Query())) { f.put("geometry", Geom.iterate((MultiPolygon) f.geometry()).iterator().next()); dbcol.insert((DBObject) JSON.parse(GeoJSONWriter.toString(f))); }//ww w. j a v a 2 s . c o m dbcol.ensureIndex(BasicDBObjectBuilder.start().add("geometry", "2dsphere").get()); workspace.setMapper(new GeoJSONMapper()); }
From source file:org.jeo.mongo.MongoTestData.java
License:Open Source License
public void setUp(DBCollection dbcol, MongoWorkspace workspace) throws IOException { VectorDataset data = TestData.states(); Schema schema = data.schema(); for (Feature f : data.cursor(new Query())) { Geometry g = f.geometry();/*from www . j ava2 s .co m*/ g = Geom.iterate((MultiPolygon) f.geometry()).iterator().next(); DBObject obj = new BasicDBObject(f.map()); obj.put(schema.geometry().getName(), JSON.parse(GeoJSONWriter.toString(g))); dbcol.insert(obj); } dbcol.ensureIndex(BasicDBObjectBuilder.start().add(data.schema().geometry().getName(), "2dsphere").get()); workspace.setMapper(new DefaultMapper(new Mapping().geometry("geometry"))); }
From source file:org.jeo.mongo.NestedTestData.java
License:Open Source License
@Override public void setUp(DBCollection dbcol, MongoWorkspace workspace) throws IOException { VectorDataset data = TestData.states(); for (Feature f : data.cursor(new Query())) { Geometry g = f.geometry();//ww w . ja va 2 s . c o m g = Geom.iterate((MultiPolygon) f.geometry()).iterator().next(); DBObject obj = new BasicDBObject(); DBObject geo = new BasicDBObject(); geo.put("shape", GeoJSON.toObject(g)); geo.put("center", GeoJSON.toObject(g.getCentroid())); obj.put("geo", geo); obj.put("STATE_NAME", f.get("STATE_NAME")); obj.put("STATE_ABBR", f.get("STATE_ABBR")); DBObject pop = new BasicDBObject(); pop.put("total", f.get("SAMP_POP")); pop.put("male", f.get("P_MALE")); pop.put("female", f.get("P_FEMALE")); obj.put("pop", pop); dbcol.insert(obj); } dbcol.ensureIndex(BasicDBObjectBuilder.start().add("geo.shape", "2dsphere").get()); Mapping mapping = new Mapping().geometry("geo.shape").geometry("geo.center"); workspace.setMapper(new DefaultMapper(mapping)); }
From source file:org.jewzaam.mongo.command.MongoUpsertCommand.java
License:Open Source License
@Override protected Result run() { if (upsert instanceof Prepareable) { ((Prepareable) upsert).prepare(); }/* ww w . j a va 2s . com*/ String json = converter.toJson(upsert); DBObject dbObj; if (upsert instanceof DBObject) { dbObj = (DBObject) upsert; } else { dbObj = (DBObject) JSON.parse(json); } try { db.requestStart(); DBCollection coll = db.getCollection(collectionName); if (dbObj.get("_id") != null) { BasicDBObject query = new BasicDBObject().append("_id", dbObj.get("_id")); return new Result(coll.update(query, dbObj, true, false)); } else { return new Result(coll.insert(dbObj)); } } finally { db.requestDone(); } }
From source file:org.jwebsocket.cachestorage.mongodb.MongoDBCacheStorageV1.java
License:Apache License
/** * {@inheritDoc/* ww w . jav a2 s .co m*/ * * @param aNewName * @throws java.lang.Exception */ @Override public void setName(String aNewName) throws Exception { mDatabase.createCollection(aNewName, null); DBCollection newCollection = mDatabase.getCollection(aNewName); DBCursor lRecords = mCollection.find(); while (lRecords.hasNext()) { newCollection.insert(lRecords.next()); } mCollection.drop(); mCollection = newCollection; mName = aNewName; }
From source file:org.jwebsocket.storage.mongodb.MongoDBStorageV1.java
License:Apache License
/** * {@inheritDoc//from w w w . j a va2 s . co m * * @param aNewName * @throws java.lang.Exception */ @Override public void setName(String aNewName) throws Exception { Assert.isTrue(null != mName, "The 'newName', argument cannot be null!"); mDatabase.createCollection(aNewName, null); DBCollection lNewCollection = mDatabase.getCollection(aNewName); DBCursor lRecords = mCollection.find(); while (lRecords.hasNext()) { lNewCollection.insert(lRecords.next()); } mCollection.drop(); mCollection = lNewCollection; mName = aNewName; }
From source file:org.knowrob.knowrob_sim_games.MongoSimGames.java
License:Open Source License
/** * Query the Pose of the given model at the given timepoint (or the most recent one) * save result to MongoDB//from w w w . j a va2 s. co m */ public void WriteModelPoseAt(double timestamp, String model_name, String pose_traj_db_name, String pose_traj_coll_name) { // $and list for querying the $match in the aggregation BasicDBList time_and_name = new BasicDBList(); // add the timestamp and the model name time_and_name.add(new BasicDBObject("timestamp", new BasicDBObject("$lte", timestamp))); time_and_name.add(new BasicDBObject("models.name", model_name)); // 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 models in order to output only the queried model DBObject unwind_models = new BasicDBObject("$unwind", "$models"); // $match for the given model name from the unwinded models DBObject match_model = new BasicDBObject("$match", new BasicDBObject("models.name", model_name)); // build the $projection operation DBObject proj_fields = new BasicDBObject("_id", 0); proj_fields.put("timestamp", 1); proj_fields.put("pos", "$models.pos"); proj_fields.put("rot", "$models.rot"); DBObject project = new BasicDBObject("$project", proj_fields); // run aggregation List<DBObject> pipeline = Arrays.asList(match_time_and_name, sort_desc, limit_result, unwind_models, match_model, project); AggregationOptions aggregationOptions = AggregationOptions.builder().batchSize(100) .outputMode(AggregationOptions.OutputMode.CURSOR).allowDiskUse(true).build(); Cursor cursor = this.coll.aggregate(pipeline, aggregationOptions); // writing results to a mongo collection try { // connect to client MongoClient mongoClient = new MongoClient(this.dbHost, 27017); // get the db DB pose_traj_db = mongoClient.getDB(pose_traj_db_name); // check if the collection already exists if (pose_traj_db.collectionExists(pose_traj_coll_name)) { System.out.println("!!! Collection: \'" + pose_traj_coll_name + "\' already exists!"); } // create the collection else { // create collection DBCollection pose_traj_coll = pose_traj_db.getCollection(pose_traj_coll_name); System.out.println("Writing most recent pose to \'" + pose_traj_coll_name + "\'"); // if query has a response, append metadata to it if (cursor.hasNext()) { // create metadata doc BasicDBObject meta_data = new BasicDBObject("name", pose_traj_coll_name) .append("type", "trajectory").append("timestamp", timestamp) .append("description", "Model pose query.."); // get the first document as the next cursor and append the metadata to it BasicDBObject first_doc = (BasicDBObject) cursor.next(); first_doc.append("metadata", meta_data); // insert document with metadata pose_traj_coll.insert(first_doc); } } } catch (UnknownHostException e) { e.printStackTrace(); } }
From source file:org.knowrob.knowrob_sim_games.MongoSimGames.java
License:Open Source License
/** * Query the Pose of the given link at the given timepoint (or the most recent one) * save result in MongoDB/* w w w . j a v a 2 s . co m*/ */ public void WriteLinkPoseAt(double timestamp, String model_name, String link_name, String pose_traj_db_name, String pose_traj_coll_name) { // $and list for querying the $match in the aggregation BasicDBList time_and_name = new BasicDBList(); // add the timestamp and the model name time_and_name.add(new BasicDBObject("timestamp", new BasicDBObject("$lte", timestamp))); time_and_name.add(new BasicDBObject("models.name", model_name)); // 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 models in order to output only the queried model DBObject unwind_models = new BasicDBObject("$unwind", "$models"); // $match for the given model name from the unwinded models DBObject match_model = new BasicDBObject("$match", new BasicDBObject("models.name", model_name)); // build the $projection operation DBObject proj_links_fields = new BasicDBObject("_id", 0); proj_links_fields.put("timestamp", 1); proj_links_fields.put("models.links", 1); DBObject project_links = new BasicDBObject("$project", proj_links_fields); // $unwind the links DBObject unwind_links = new BasicDBObject("$unwind", "$models.links"); // $match for the given link name from the unwinded links DBObject match_link = new BasicDBObject("$match", new BasicDBObject("models.links.name", link_name)); // build the final $projection operation DBObject proj_fields = new BasicDBObject("timestamp", 1); proj_fields.put("pos", "$models.links.pos"); proj_fields.put("rot", "$models.links.rot"); DBObject project = new BasicDBObject("$project", proj_fields); // run aggregation List<DBObject> pipeline = Arrays.asList(match_time_and_name, sort_desc, limit_result, unwind_models, match_model, project_links, unwind_links, match_link, project); AggregationOptions aggregationOptions = AggregationOptions.builder().batchSize(100) .outputMode(AggregationOptions.OutputMode.CURSOR).allowDiskUse(true).build(); Cursor cursor = this.coll.aggregate(pipeline, aggregationOptions); // writing results to a mongo collection try { // connect to client MongoClient mongoClient = new MongoClient(this.dbHost, 27017); // get the db DB pose_traj_db = mongoClient.getDB(pose_traj_db_name); // check if the collection already exists if (pose_traj_db.collectionExists(pose_traj_coll_name)) { System.out.println("!!! Collection: \'" + pose_traj_coll_name + "\' already exists!"); } // create the collection else { // create collection DBCollection pose_traj_coll = pose_traj_db.getCollection(pose_traj_coll_name); System.out.println("Writing most recent pose to \'" + pose_traj_coll_name + "\'"); // if query has a response, append metadata to it if (cursor.hasNext()) { // create metadata doc BasicDBObject meta_data = new BasicDBObject("name", pose_traj_coll_name) .append("type", "trajectory").append("timestamp", timestamp) .append("description", "Link pose query.."); // get the first document as the next cursor and append the metadata to it BasicDBObject first_doc = (BasicDBObject) cursor.next(); first_doc.append("metadata", meta_data); // insert document with metadata pose_traj_coll.insert(first_doc); } } } catch (UnknownHostException e) { e.printStackTrace(); } }
From source file:org.knowrob.knowrob_sim_games.MongoSimGames.java
License:Open Source License
/** * Query the Pose of the given collision at the given timepoint (or the most recent one) * save result in MongoDB// ww w . j av a2 s.c o m */ public void WriteCollisionPoseAt(double timestamp, String model_name, String link_name, String collision_name, String pose_traj_db_name, String pose_traj_coll_name) { // $and list for querying the $match in the aggregation BasicDBList time_and_name = new BasicDBList(); // add the timestamp and the model name time_and_name.add(new BasicDBObject("timestamp", new BasicDBObject("$lte", timestamp))); time_and_name.add(new BasicDBObject("models.name", model_name)); // 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 models in order to output only the queried model DBObject unwind_models = new BasicDBObject("$unwind", "$models"); // $match for the given model name from the unwinded models DBObject match_model = new BasicDBObject("$match", new BasicDBObject("models.name", model_name)); // build the $projection operation DBObject proj_links_fields = new BasicDBObject("_id", 0); proj_links_fields.put("timestamp", 1); proj_links_fields.put("models.links", 1); DBObject project_links = new BasicDBObject("$project", proj_links_fields); // $unwind the links DBObject unwind_links = new BasicDBObject("$unwind", "$models.links"); // $match for the given link name from the unwinded links DBObject match_link = new BasicDBObject("$match", new BasicDBObject("models.links.name", link_name)); // build the final $projection operation DBObject proj_collision_fields = new BasicDBObject("timestamp", 1); proj_collision_fields.put("models.links.collisions", 1); DBObject project_collisions = new BasicDBObject("$project", proj_collision_fields); // $unwind the collisions DBObject unwind_collisions = new BasicDBObject("$unwind", "$models.links.collisions"); // $match for the given collision name from the unwinded collisions DBObject match_collision = new BasicDBObject("$match", new BasicDBObject("models.links.collisions.name", collision_name)); // build the final $projection operation DBObject proj_fields = new BasicDBObject("timestamp", 1); proj_fields.put("pos", "$models.links.collisions.pos"); proj_fields.put("rot", "$models.links.collisions.rot"); DBObject project = new BasicDBObject("$project", proj_fields); // run aggregation List<DBObject> pipeline = Arrays.asList(match_time_and_name, sort_desc, limit_result, unwind_models, match_model, project_links, unwind_links, match_link, project_collisions, unwind_collisions, match_collision, project); AggregationOptions aggregationOptions = AggregationOptions.builder().batchSize(100) .outputMode(AggregationOptions.OutputMode.CURSOR).allowDiskUse(true).build(); Cursor cursor = this.coll.aggregate(pipeline, aggregationOptions); // writing results to a mongo collection try { // connect to client MongoClient mongoClient = new MongoClient(this.dbHost, 27017); // get the db DB pose_traj_db = mongoClient.getDB(pose_traj_db_name); // check if the collection already exists if (pose_traj_db.collectionExists(pose_traj_coll_name)) { System.out.println("!!! Collection: \'" + pose_traj_coll_name + "\' already exists!"); } // create the collection else { // create collection DBCollection pose_traj_coll = pose_traj_db.getCollection(pose_traj_coll_name); System.out.println("Writing most recent pose to \'" + pose_traj_coll_name + "\'"); // if query has a response, append metadata to it if (cursor.hasNext()) { // create metadata doc BasicDBObject meta_data = new BasicDBObject("name", pose_traj_coll_name) .append("type", "trajectory").append("timestamp", timestamp) .append("description", "Collision pose query.."); // get the first document as the next cursor and append the metadata to it BasicDBObject first_doc = (BasicDBObject) cursor.next(); first_doc.append("metadata", meta_data); // insert document with metadata pose_traj_coll.insert(first_doc); } } } catch (UnknownHostException e) { e.printStackTrace(); } }
From source file:org.knowrob.knowrob_sim_games.MongoSimGames.java
License:Open Source License
/** * Query the trajectory of the given model with double timestamps * save result in MongoDB/*from w w w. ja v a2 s.co m*/ */ public void WriteModelTrajectory(double start_ts, double end_ts, String model_name, String traj_db_name, String traj_coll_name) { // 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 models DBObject unwind_models = new BasicDBObject("$unwind", "$models"); // $match for the given model name from the unwinded models DBObject match_model = new BasicDBObject("$match", new BasicDBObject("models.name", model_name)); // build the $projection operation DBObject proj_fields = new BasicDBObject("_id", 0); proj_fields.put("timestamp", 1); proj_fields.put("pos", "$models.pos"); proj_fields.put("rot", "$models.rot"); DBObject project = new BasicDBObject("$project", proj_fields); // run aggregation List<DBObject> pipeline = Arrays.asList(match_time, unwind_models, match_model, project); AggregationOptions aggregationOptions = AggregationOptions.builder().batchSize(100) .outputMode(AggregationOptions.OutputMode.CURSOR).allowDiskUse(true).build(); Cursor cursor = this.coll.aggregate(pipeline, aggregationOptions); // writing results to a mongo collection try { // connect to client MongoClient mongoClient = new MongoClient(this.dbHost, 27017); // get the db DB traj_db = mongoClient.getDB(traj_db_name); // check if the collection already exists if (traj_db.collectionExists(traj_coll_name)) { System.out.println("!!! Collection: \'" + traj_coll_name + "\' already exists!"); } // create the collection else { // create collection DBCollection traj_coll = traj_db.getCollection(traj_coll_name); System.out.println("Java - Writing to \'" + traj_db_name + "." + traj_coll_name + "\'"); // if cursor not empty, append matadata to the first doc if (cursor.hasNext()) { // create metadata doc BasicDBObject meta_data = new BasicDBObject("name", traj_coll_name).append("type", "trajectory") .append("start", start_ts).append("end", end_ts) .append("description", "Model trajectory.."); // get the first document as the next cursor and append the metadata to it BasicDBObject first_doc = (BasicDBObject) cursor.next(); first_doc.append("metadata", meta_data); // insert document with metadata traj_coll.insert(first_doc); } // if query returned no values for these timestamps, get the pose at the nearest timestamp else { // write the pose to the given db and coll this.WriteModelPoseAt(start_ts, model_name, traj_db_name, traj_coll_name); } // insert rest of trajectory while (cursor.hasNext()) { traj_coll.insert(cursor.next()); } } } catch (UnknownHostException e) { e.printStackTrace(); } }