List of usage examples for com.mongodb.client MongoCollection createIndex
String createIndex(Bson keys);
From source file:bariopendatalab.db.DBAccess.java
public void createDB() { MongoDatabase database = client.getDatabase(DBNAME); database.createCollection(COLLNAME); MongoCollection<Document> collection = database.getCollection(COLLNAME); collection.createIndex(new BsonDocument("geometry", new BsonString("2dsphere"))); }
From source file:com.bc.fiduceo.db.MongoDbDriver.java
License:Open Source License
@Override public void initialize() throws SQLException { final MongoCollection<Document> satelliteObservations = database.getCollection(SATELLITE_DATA_COLLECTION); satelliteObservations.createIndex(new BasicDBObject(START_TIME_KEY, 1)); satelliteObservations.createIndex(new BasicDBObject(STOP_TIME_KEY, 1)); satelliteObservations.createIndex(new BasicDBObject(SENSOR_KEY + ".name", 1)); }
From source file:com.gs.obevo.mongodb.impl.MongoDbChangeAuditDao.java
License:Apache License
@Override public void init() { for (PhysicalSchema physicalSchema : env.getPhysicalSchemas()) { MongoDatabase database = mongoClient.getDatabase(physicalSchema.getPhysicalName()); try {/*from w w w. j av a2 s. c o m*/ database.createCollection(getAuditContainerName()); } catch (Exception e) { // create if it doesn't exist already; TODO clean this up } MongoCollection<Document> collection = database.getCollection(getAuditContainerName()); collection.createIndex(Indexes.ascending(changeNameColumn, "OBJECTNAME")); } }
From source file:com.ibm.research.mongotx.daytrader.Load.java
License:Open Source License
private static void createCollections(MongoDatabase db) throws Exception { db.createCollection(COL_QUOTE);// ww w .j a v a 2 s. c om db.createCollection(COL_HOLDING); MongoCollection<Document> holdingCol = db.getCollection(COL_HOLDING); holdingCol.createIndex(new Document(H_ACCOUNT_ACCOUNTID, true)); db.createCollection(COL_ACCOUNTPROFILE); db.createCollection(COL_ACCOUNT); MongoCollection<Document> accountCol = db.getCollection(COL_ACCOUNT); accountCol.createIndex(new Document(A_PROFILE_USERID, true)); db.createCollection(COL_ORDER); MongoCollection<Document> orderCol = db.getCollection(COL_ORDER); orderCol.createIndex(new Document(O_ACCOUNT_ACCOUNTID, true)); }
From source file:com.mycompany.citysearchnosql.Executioner.java
public static void main(final String[] args) { // 1. Connect to MongoDB instance running on localhost MongoClient mongoClient = new MongoClient(); // Access database named 'test' MongoDatabase database = mongoClient.getDatabase("test"); // Access collection named 'restaurants' MongoCollection<Document> collection = database.getCollection("restaurants"); // 2. Insert List<Document> documents = asList( new Document("name", "Sun Bakery Trattoria").append("stars", 4).append("categories", asList("Pizza", "Pasta", "Italian", "Coffee", "Sandwiches")), new Document("name", "Blue Bagels Grill").append("stars", 3).append("categories", asList("Bagels", "Cookies", "Sandwiches")), new Document("name", "Hot Bakery Cafe").append("stars", 4).append("categories", asList("Bakery", "Cafe", "Coffee", "Dessert")), new Document("name", "XYZ Coffee Bar").append("stars", 5).append("categories", asList("Coffee", "Cafe", "Bakery", "Chocolates")), new Document("name", "456 Cookies Shop").append("stars", 4).append("categories", asList("Bakery", "Cookies", "Cake", "Coffee"))); collection.insertMany(documents);//w w w.j av a2s .c o m // 3. Query List<Document> results = collection.find().into(new ArrayList<>()); // 4. Create Index collection.createIndex(Indexes.ascending("name")); // 5. Perform Aggregation collection.aggregate(asList(match(eq("categories", "Bakery")), group("$stars", sum("count", 1)))); mongoClient.close(); }
From source file:connector.DBConnector.java
public static void StartConnection() { try {/*from ww w.j a va 2 s. c o m*/ // To connect to mongodb server MongoClient mongoClient = new MongoClient(host, port); database = mongoClient.getDatabase("test"); MongoCollection<Document> coll = database.getCollection("tweets"); coll.createIndex(Indexes.text("text")); System.out.println("Connect to database successfully : " + database.getName()); } catch (Exception e) { System.err.println(e.getClass().getName() + ": " + e.getMessage()); } }
From source file:io.djigger.collector.accessors.stackref.AbstractAccessor.java
License:Open Source License
protected void createOrUpdateIndex(MongoCollection<Document> collection, String attribute) { Document index = getIndex(collection, attribute); if (index == null) { collection.createIndex(new Document(attribute, 1)); }// w w w .ja v a 2 s .co m }
From source file:io.sip3.tapir.salto.cron.SipMessageMongoCron.java
License:Apache License
private void ensureIndexes(String suffix) { MongoCollection index = db.getCollection("index_" + suffix); index.createIndex(new Document(Field.millis, 1)); index.createIndex(new Document(Field.call_id, "hashed")); index.createIndex(new Document(Field.caller, "hashed")); index.createIndex(new Document(Field.callee, "hashed")); MongoCollection raw = db.getCollection("raw_" + suffix); raw.createIndex(new Document(Field.call_id, "hashed")); }
From source file:it.terrinoni.Controller.java
public static void main(String[] args) { MongoClient client = new MongoClient(); MongoDatabase database = client.getDatabase("photo-sharing"); MongoCollection<Document> albums = database.getCollection("albums"); MongoCollection<Document> images = database.getCollection("images"); albums.createIndex(new Document("images", 1)); // Get the iterator of the whole collection MongoCursor<Document> cursor = images.find().iterator(); try {//www. j a va2 s . c om while (cursor.hasNext()) { Document currImg = cursor.next(); Document foundImg = albums.find(eq("images", currImg.getDouble("_id"))).first(); if (foundImg == null) { //System.out.println(currImg.getDouble("_id") + " deleted."); images.deleteOne(currImg); } //System.out.println(currImg.getDouble("_id") + " is ok."); } } finally { cursor.close(); } long numImgs = images.count(eq("tags", "sunrises")); System.out.println("The total number of images with the tag \"sunrises\" after the removal of orphans is: " + String.valueOf(numImgs)); }
From source file:mongodb.QuickTourAdmin.java
License:Apache License
/** * Run this main method to see the output of this quick example. * * @param args//from w w w . j a v a 2 s .c o m * takes an optional single argument for the connection string */ public static void main(final String[] args) { MongoClient mongoClient; if (args.length == 0) { // connect to the specified database server mongoClient = new MongoClient("10.9.17.105", 27017); } else { mongoClient = new MongoClient(new MongoClientURI(args[0])); } // get handle to "test" database MongoDatabase database = mongoClient.getDatabase("test"); database.drop(); // get a handle to the "test" collection MongoCollection<Document> collection = database.getCollection("test"); // drop all the data in it collection.drop(); // getting a list of databases for (String name : mongoClient.listDatabaseNames()) { System.out.println(name); } // drop a database mongoClient.getDatabase("databaseToBeDropped").drop(); // create a collection database.createCollection("cappedCollection", new CreateCollectionOptions().capped(true).sizeInBytes(0x100000)); for (String name : database.listCollectionNames()) { System.out.println(name); } // drop a collection: collection.drop(); // create an ascending index on the "i" field // 1 ascending or -1 for descending collection.createIndex(new Document("i", 1)); // list the indexes on the collection for (final Document index : collection.listIndexes()) { System.out.println(index.toJson()); } // create a text index on the "content" field // text indexes to support text search of string content collection.createIndex(new Document("content", "text")); collection.insertOne(new Document("_id", 0).append("content", "textual content")); collection.insertOne(new Document("_id", 1).append("content", "additional content")); collection.insertOne(new Document("_id", 2).append("content", "irrelevant content")); // Find using the text index long matchCount = collection.count(text("textual content -irrelevant")); System.out.println("Text search matches: " + matchCount); // Find using the $language operator Bson textSearch = text("textual content -irrelevant", "english"); matchCount = collection.count(textSearch); System.out.println("Text search matches (english): " + matchCount); // Find the highest scoring match Document projection = new Document("score", new Document("$meta", "textScore")); Document myDoc = collection.find(textSearch).projection(projection).first(); System.out.println("Highest scoring document: " + myDoc.toJson()); // Run a command Document buildInfo = database.runCommand(new Document("buildInfo", 1)); System.out.println(buildInfo); // release resources database.drop(); mongoClient.close(); }