List of usage examples for com.mongodb.client MongoDatabase getCollection
MongoCollection<Document> getCollection(String collectionName);
From source file:de.dfki.mmf.examples.craft_task_example.MongoDBWorldDescription.java
License:Open Source License
public MongoDatabase createCraftTaskDatabase() { MongoClient mongoClient = new MongoClient(); MongoDatabase db = mongoClient.getDatabase("craftdb"); db.getCollection("User") .insertOne(new Document("worldObjectId", "User1") .append("worldobjecttype", "User").append("position", new Document() .append("xPosition", "1.1").append("yPosition", "0.25").append("zPosition", "0.2")) .append("name", "Magdalena")); db.getCollection("Robot") .insertOne(new Document("worldObjectId", "Robot1") .append("worldobjecttype", "Robot").append("position", new Document() .append("xPosition", "0.0").append("yPosition", "0.0").append("zPosition", "0.0")) .append("name", "Nao")); db.getCollection("Toolbox") .insertOne(new Document("worldObjectId", "Toolbox1").append("worldobjecttype", "Toolbox") .append("position", new Document().append("xPosition", "0.12").append("yPosition", "-0.22") .append("zPosition", "0.0")) .append("content", asList(new Document().append("toolName", "Drill").append("amount", "1"), new Document().append("toolName", "Screwdriver").append("amount", "3"), new Document().append("toolName", "Knife").append("amount", "5"), new Document().append("toolName", "Hammer").append("amount", "1"), new Document().append("toolName", "Sponge").append("amount", "2"), new Document().append("toolName", "Scissors").append("amount", "2")) ));//from w w w . jav a2s . c o m db.getCollection("Hammer") .insertOne( new Document("worldObjectId", "Hammer1").append("worldobjecttype", "Hammer") .append("position", new Document().append("xPosition", "0.57") .append("yPosition", "-0.20").append("zPosition", "0.03")) .append("belongsTo", "Toolbox1")); db.getCollection("Scissors") .insertOne(new Document("worldObjectId", "Scissors1").append("worldobjecttype", "Scissors") .append("color", "black").append("size", "big").append("position", new Document() .append("xPosition", "0.18").append("yPosition", "0.3").append("zPosition", "0.05")) .append("belongsTo", "Toolbox1")); db.getCollection("Scissors") .insertOne(new Document("worldObjectId", "Scissors2").append("worldobjecttype", "Scissors") .append("color", "blue-red").append("size", "small").append("position", new Document() .append("xPosition", "0.6").append("yPosition", "0.25").append("zPosition", "0.01")) .append("belongsTo", "Toolbox1")); db.getCollection("Sponge") .insertOne(new Document("worldObjectId", "Sponge1").append("worldobjecttype", "Sponge") .append("color", "yellow").append("position", new Document().append("xPosition", "0.5") .append("yPosition", "0.4").append("zPosition", "0.01")) .append("belongsTo", "Toolbox1")); db.getCollection("Sponge") .insertOne(new Document("worldObjectId", "Sponge2").append("worldobjecttype", "Sponge") .append("color", "green").append("position", new Document().append("xPosition", "0.25") .append("yPosition", "-0.18").append("zPosition", "-0.05")) .append("belongsTo", "Toolbox1")); return db; }
From source file:de.dfki.mmf.examples.craft_task_example.MongoDBWorldDescription.java
License:Open Source License
public MongoDatabase clearDatabase(String dbName) { MongoClient mongoClient = new MongoClient(); MongoDatabase db = mongoClient.getDatabase(dbName); MongoIterable<String> collectionNames = db.listCollectionNames(); for (String name : collectionNames) { db.getCollection(name).drop(); }/* w w w.j a v a2s .com*/ return db; }
From source file:de.dfki.mmf.examples.craft_task_example.MongoDBWorldDescription.java
License:Open Source License
public void printAllDatabaseEntries(MongoDatabase db) { MongoIterable<String> collectionNames = db.listCollectionNames(); for (String name : collectionNames) { FindIterable<Document> iterable = db.getCollection(name).find(); iterable.forEach(new Block<Document>() { public void apply(final Document document) { System.out.println(document.toJson()); }/*from ww w . ja v a2s. c o m*/ }); System.out.println("----------------------"); } }
From source file:de.dfki.mmf.examples.messy_workingplace_example.MongoDBWorldDescription.java
License:Open Source License
public MongoDatabase createUserStudyDatabase() { MongoClient mongoClient = new MongoClient(); MongoDatabase db = mongoClient.getDatabase("userstudydb"); db.getCollection("Person").insertOne(new Document("worldObjectId", "Person1") .append("worldobjecttype", "Person").append("name", "Magdalena")); db.getCollection("Person") .insertOne(new Document("worldObjectId", "Person2").append("worldobjecttype", "Person")); db.getCollection("User").insertOne(new Document("worldObjectId", "User1").append("worldobjecttype", "User") .append("name", "Magdalena")); db.getCollection("User") .insertOne(new Document("worldObjectId", "User2").append("worldobjecttype", "User") .append("position", new Document().append("xPosition", "0.8").append("yPosition", "0.0") .append("zPosition", "0.13"))); db.getCollection("Robot") .insertOne(new Document("worldObjectId", "Robot1") .append("worldobjecttype", "Robot").append("position", new Document() .append("xPosition", "0.0").append("yPosition", "0.0").append("zPosition", "0.0")) .append("name", "Nao")); db.getCollection("Scissors") .insertOne(new Document("worldObjectId", "Scissors1").append("worldobjecttype", "Scissors") .append("color", "black").append("size", "big") .append("position", new Document().append("xPosition", "0.27").append("yPosition", "-0.85") .append("zPosition", "-0.15"))); db.getCollection("Scissors") .insertOne(new Document("worldObjectId", "Scissors2").append("worldobjecttype", "Scissors") .append("color", "blue and red").append("size", "small") .append("position", new Document().append("xPosition", "0.6").append("yPosition", "-0.55") .append("zPosition", "-0.15"))); db.getCollection("Pen") .insertOne(new Document("worldObjectId", "MarkerPen1").append("worldobjecttype", "MarkerPen") .append("color", "pink").append("size", "big").append("label", "Rex Textmarker") .append("position", new Document().append("xPosition", "0.68").append("yPosition", "-0.3") .append("zPosition", "-0.12"))); db.getCollection("Pen") .insertOne(new Document("worldObjectId", "MarkerPen2").append("worldobjecttype", "MarkerPen") .append("color", "yellow").append("size", "big").append("label", "Stabilo") .append("position", new Document().append("xPosition", "0.39").append("yPosition", "-0.32") .append("zPosition", "-0.12"))); db.getCollection("Pen") .insertOne(new Document("worldObjectId", "MarkerPen3").append("worldobjecttype", "MarkerPen") .append("color", "pink").append("size", "small").append("label", "Stabilo") .append("position", new Document().append("xPosition", "0.39").append("yPosition", "0.26") .append("zPosition", "-0.22"))); db.getCollection("Pen") .insertOne(new Document("worldObjectId", "MarkerPen4").append("worldobjecttype", "MarkerPen") .append("color", "green").append("size", "small") .append("position", new Document().append("xPosition", "0.5").append("yPosition", "-0.61") .append("zPosition", "-0.15"))); db.getCollection("Pen") .insertOne(new Document("worldObjectId", "MarkerPen5").append("worldobjecttype", "MarkerPen") .append("color", "yellow").append("size", "big").append("label", "Pelikan")); db.getCollection("Pen").insertOne(new Document("worldObjectId", "MarkerPen6") .append("worldobjecttype", "MarkerPen").append("color", "orange").append("size", "big")); db.getCollection("Pen").insertOne(new Document("worldObjectId", "MarkerPen7") .append("worldobjecttype", "MarkerPen").append("color", "pink").append("size", "big")); db.getCollection("Pen") .insertOne(new Document("worldObjectId", "ColoredPencil1") .append("worldobjecttype", "ColoredPencil").append("color", "purple") .append("size", "small").append("position", new Document().append("xPosition", "0.36") .append("yPosition", "-0.58").append("zPosition", "-0.20"))); db.getCollection("Pen") .insertOne(new Document("worldObjectId", "ColoredPencil2") .append("worldobjecttype", "ColoredPencil").append("color", "blue").append("size", "medium") .append("position", new Document().append("xPosition", "0.53").append("yPosition", "-0.275") .append("zPosition", "-0.12"))); db.getCollection("Pen").insertOne(new Document("worldObjectId", "ColoredPencil3") .append("worldobjecttype", "ColoredPencil").append("color", "red").append("size", "small")); db.getCollection("Pen").insertOne(new Document("worldObjectId", "ColoredPencil4") .append("worldobjecttype", "ColoredPencil").append("color", "green").append("size", "medium")); db.getCollection("Pen").insertOne(new Document("worldObjectId", "ColoredPencil5") .append("worldobjecttype", "ColoredPencil").append("color", "orange").append("size", "big")); db.getCollection("Pen").insertOne(new Document("worldObjectId", "ColoredPencil6") .append("worldobjecttype", "ColoredPencil").append("color", "purple").append("size", "big")); db.getCollection("Pen") .insertOne(new Document("worldObjectId", "BallPen1").append("worldobjecttype", "BallPen") .append("color", "blue").append("label", "Mathema") .append("approximate Position", "on your left")); db.getCollection("Pen").insertOne(new Document("worldObjectId", "BallPen2") .append("worldobjecttype", "BallPen").append("color", "blue").append("label", "Mathema") .append("approximate Position", "on your far left").append("position", new Document() .append("xPosition", "0.32").append("yPosition", "-0.45").append("zPosition", "-0.12"))); db.getCollection("Pen") .insertOne(new Document("worldObjectId", "BallPen3").append("worldobjecttype", "BallPen") .append("color", "yellow").append("label", "pizza") .append("position", new Document().append("xPosition", "0.32").append("yPosition", "-0.38") .append("zPosition", "-0.22"))); db.getCollection("Pen").insertOne(new Document("worldObjectId", "BallPen4") .append("worldobjecttype", "BallPen").append("color", "yellow")); db.getCollection("Pen") .insertOne(new Document("worldObjectId", "BallPen5").append("worldobjecttype", "BallPen") .append("color", "blue").append("approximate position", "on your far left")); db.getCollection("Pen").insertOne(new Document("worldObjectId", "BallPen6") .append("worldobjecttype", "BallPen").append("color", "white").append("label", "KTH")); db.getCollection("Pen").insertOne(new Document("worldObjectId", "BallPen7") .append("worldobjecttype", "BallPen").append("color", "red")); db.getCollection("Paper") .insertOne(new Document("worldObjectId", "Paper1").append("worldobjecttype", "Paper") .append("position", new Document().append("xPosition", "0.63").append("yPosition", "0.15") .append("zPosition", "-0.2"))); db.getCollection("Plate") .insertOne(new Document("worldObjectId", "Plate1").append("worldobjecttype", "Plate") .append("material", "paper").append("color", "orange").append("motif", "white dots") .append("position", new Document().append("xPosition", "0.33").append("yPosition", "0.56") .append("zPosition", "-0.15"))); db.getCollection("Plate") .insertOne(new Document("worldObjectId", "Plate2").append("worldobjecttype", "Plate") .append("material", "paper").append("color", "grey white").append("motif", "zig-zag line") .append("position", new Document().append("xPosition", "0.27").append("yPosition", "-0.85") .append("zPosition", "-0.15"))); db.getCollection("Plate") .insertOne(new Document("worldObjectId", "Plate3").append("worldobjecttype", "Plate") .append("material", "ceramic").append("color", "white") .append("position", new Document().append("xPosition", "0.6").append("yPosition", "-0.87") .append("zPosition", "-0.12"))); db.getCollection("Cup").insertOne(new Document("worldObjectId", "Cup1").append("worldobjecttype", "Cup") .append("print", "TECS").append("color", "white").append("position", new Document() .append("xPosition", "0.45").append("yPosition", "0.35").append("zPosition", "-0.15"))); db.getCollection("Cup") .insertOne(new Document("worldObjectId", "Cup2").append("worldobjecttype", "Cup") .append("color", "blue").append("position", new Document().append("xPosition", "0.14") .append("yPosition", "0.48").append("zPosition", "-0.15"))); return db;/*from ww w.j a v a 2s . c om*/ }
From source file:de.dfki.mmf.input.worldmodel.WorldModelFactory.java
License:Open Source License
/** * * @param saliencyAnnotation//from www .j av a2 s. c om * @param robotModel * @param databaseName * Retrieve object properties from a MongoDB database */ private void mongoDBModelRetrieval(JsonObject saliencyAnnotation, RobotModel robotModel, String databaseName) { MongoClient mongoClient = new MongoClient(); MongoDatabase db = mongoClient.getDatabase(databaseName); MongoIterable<String> collectionNames = db.listCollectionNames(); ArrayList<JsonObject> worldProperties = new ArrayList<>(); for (String name : collectionNames) { FindIterable<Document> iterable = db.getCollection(name).find(); iterable.forEach(new Block<Document>() { public void apply(final Document document) { JsonObject jsonObject = (new JsonParser()).parse(document.toJson()).getAsJsonObject(); worldProperties.add(jsonObject); } }); } postProcessWorldModel(worldProperties, saliencyAnnotation, robotModel); }
From source file:documentation.CausalConsistencyExamples.java
License:Apache License
private static void setupDatabase() { MongoClient client = MongoClients.create(); client.getDatabase("test").drop(); MongoDatabase database = client.getDatabase("test"); database.getCollection("items").drop(); MongoCollection<Document> items = database.getCollection("items"); Document document = new Document("sku", "111").append("name", "Peanuts").append("start", new Date()); items.insertOne(document);/*from w ww .ja va 2s.c o m*/ client.close(); }
From source file:documentation.ChangeStreamSamples.java
License:Apache License
/** * Run this main method to see the output of this quick example. * * @param args takes an optional single argument for the connection string */// ww w.j a v a 2 s.c o m public static void main(final String[] args) { MongoClient mongoClient; if (args.length == 0) { // connect to the local database server mongoClient = MongoClients.create("mongodb://localhost:27017,localhost:27018,localhost:27019"); } else { mongoClient = MongoClients.create(args[0]); } // Select the MongoDB database. MongoDatabase database = mongoClient.getDatabase("testChangeStreams"); database.drop(); sleep(); // Select the collection to query. MongoCollection<Document> collection = database.getCollection("documents"); /* * Example 1 * Create a simple change stream against an existing collection. */ System.out.println("1. Initial document from the Change Stream:"); // Create the change stream cursor. MongoChangeStreamCursor<ChangeStreamDocument<Document>> cursor = collection.watch().cursor(); // Insert a test document into the collection. collection.insertOne(Document.parse("{username: 'alice123', name: 'Alice'}")); ChangeStreamDocument<Document> next = cursor.next(); System.out.println(next); cursor.close(); sleep(); /* * Example 2 * Create a change stream with 'lookup' option enabled. * The test document will be returned with a full version of the updated document. */ System.out.println("2. Document from the Change Stream, with lookup enabled:"); // Create the change stream cursor. cursor = collection.watch().fullDocument(FullDocument.UPDATE_LOOKUP).cursor(); // Update the test document. collection.updateOne(Document.parse("{username: 'alice123'}"), Document.parse("{$set : { email: 'alice@example.com'}}")); // Block until the next result is returned next = cursor.next(); System.out.println(next); cursor.close(); sleep(); /* * Example 3 * Create a change stream with 'lookup' option using a $match and ($redact or $project) stage. */ System.out.println( "3. Document from the Change Stream, with lookup enabled, matching `update` operations only: "); // Insert some dummy data. collection.insertMany(asList(Document.parse("{updateMe: 1}"), Document.parse("{replaceMe: 1}"))); // Create $match pipeline stage. List<Bson> pipeline = singletonList( Aggregates.match(Filters.or(Document.parse("{'fullDocument.username': 'alice123'}"), Filters.in("operationType", asList("update", "replace", "delete"))))); // Create the change stream cursor with $match. cursor = collection.watch(pipeline).fullDocument(FullDocument.UPDATE_LOOKUP).cursor(); // Forward to the end of the change stream next = cursor.tryNext(); // Update the test document. collection.updateOne(Filters.eq("updateMe", 1), Updates.set("updated", true)); next = cursor.next(); System.out.println(format("Update operationType: %s %n %s", next.getUpdateDescription(), next)); // Replace the test document. collection.replaceOne(Filters.eq("replaceMe", 1), Document.parse("{replaced: true}")); next = cursor.next(); System.out.println(format("Replace operationType: %s", next)); // Delete the test document. collection.deleteOne(Filters.eq("username", "alice123")); next = cursor.next(); System.out.println(format("Delete operationType: %s", next)); cursor.close(); sleep(); /** * Example 4 * Resume a change stream using a resume token. */ System.out.println("4. Document from the Change Stream including a resume token:"); // Get the resume token from the last document we saw in the previous change stream cursor. BsonDocument resumeToken = cursor.getResumeToken(); System.out.println(resumeToken); // Pass the resume token to the resume after function to continue the change stream cursor. cursor = collection.watch().resumeAfter(resumeToken).cursor(); // Insert a test document. collection.insertOne(Document.parse("{test: 'd'}")); // Block until the next result is returned next = cursor.next(); System.out.println(next); cursor.close(); }
From source file:dummydata.DummyData.java
private static Map GenerateNodeMacAddress() { MongoClient mongoClient = new MongoClient("129.217.152.20", 27017); MongoDatabase db = mongoClient.getDatabase("autophy-dev"); MongoCollection<Document> collection = db.getCollection("macAddrs"); FindIterable<Document> iterable = collection.find(); Document document = iterable.first(); document.remove("_id"); Map<String, String> documentMap = (Map) document; return documentMap; }
From source file:edu.emory.bmi.datacafe.mongo.MongoCollection.java
License:Open Source License
/** * Get a collection with a selected set of attributes * * @param document the interested attributes * @return an iterable document.// www . j a va 2s . c o m */ public FindIterable<Document> getCollection(Document document) { MongoDatabase db = mongoClient.getDatabase(database); return db.getCollection(collection).find(document); }
From source file:edu.emory.bmi.datacafe.mongo.MongoCollection.java
License:Open Source License
/** * Iterates a collection in a given database. * * @return an iterable document./*from w w w. j av a 2 s . c o m*/ */ public FindIterable<Document> iterateCollection() { MongoDatabase db = mongoClient.getDatabase(database); return db.getCollection(collection).find(); }