List of usage examples for com.mongodb.client MongoDatabase createCollection
void createCollection(String collectionName);
From source file:com.telefonica.iot.cygnus.backends.mongo.MongoBackendImpl.java
License:Open Source License
/** * Creates a collection for STH, given its name, if not exists in the given database. Time-based limits are set, * if possible.//from ww w .j a v a 2 s. co m * @param dbName * @param collectionName * @param dataExpiration * @throws Exception */ @Override public void createCollection(String dbName, String collectionName, long dataExpiration) throws Exception { LOGGER.debug("Creating Mongo collection=" + collectionName + " at database=" + dbName); MongoDatabase db = getDatabase(dbName); // create the collection try { db.createCollection(collectionName); } catch (Exception e) { if (e.getMessage().contains("collection already exists")) { LOGGER.debug("Collection already exists, nothing to create"); } else { throw e; } // if else } // try catch // ensure the _id.origin index, if possible try { if (dataExpiration != 0) { BasicDBObject keys = new BasicDBObject().append("_id.origin", 1); IndexOptions options = new IndexOptions().expireAfter(dataExpiration, TimeUnit.SECONDS); db.getCollection(collectionName).createIndex(keys, options); } // if } catch (Exception e) { throw e; } // try catch }
From source file:henu.util.NosqlDB.java
public static boolean createCollection(String collectionName) { MongoDatabase db = getMongoDataBase(); try {/*from w w w . j av a2 s .c o m*/ db.createCollection(collectionName); return true; } catch (Exception e) { } return false; }
From source file:mypackage.CreateDB.java
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); try (PrintWriter out = response.getWriter()) { String getDBName = request.getParameter("dbname"); MongoClient mongoClient = new MongoClient("localhost", 27017); HttpSession httpSession = request.getSession(false); DB db = mongoClient.getDB("mydb"); DBCollection dBCollection = db.getCollection(httpSession.getAttribute("uname") + "DB"); BasicDBObject dBObject = new BasicDBObject(); dBObject.put("kapil", getDBName); WriteResult writeResult = dBCollection.insert(dBObject); if (writeResult.getN() == 0) { out.print("true"); MongoDatabase mongoDatabase = mongoClient.getDatabase(getDBName); mongoDatabase.createCollection("Welcome"); mongoDatabase.drop();/*from w ww . j av a 2 s .c om*/ } else { out.print("false"); } } }
From source file:org.apache.drill.exec.store.mongo.MongoTestSuit.java
License:Apache License
private static void createDbAndCollections(String dbName, String collectionName, String indexFieldName) { MongoDatabase db = mongoClient.getDatabase(dbName); MongoCollection<Document> mongoCollection = db.getCollection(collectionName); if (mongoCollection == null) { db.createCollection(collectionName); mongoCollection = db.getCollection(collectionName); }/* w w w. j ava 2 s . com*/ IndexOptions indexOptions = new IndexOptions().unique(true).background(false).name(indexFieldName); Bson keys = Indexes.ascending(indexFieldName); mongoCollection.createIndex(keys, indexOptions); }
From source file:org.apache.metamodel.mongodb.mongo3.MongoDbUpdateCallback.java
License:Apache License
protected void createCollection(String name) { MongoDatabase mongoDb = _dataContext.getMongoDb(); mongoDb.createCollection(name); MongoCollection<Document> collection = mongoDb.getCollection(name); _collections.put(name, collection);/*from w ww . j a va2 s . co m*/ }
From source file:org.bananaforscale.cormac.dao.database.DatabaseDataServiceImpl.java
License:Apache License
/** * Creates a new database explicitly. Because MongoDB creates a database implicitly when the * database is first referenced in a command, this method is not required for usage of said * database.//from w w w . ja v a 2 s . c o m * * @param databaseName the database to create * @return the result of the operation * @throws DatasourceException * @throws ExistsException */ @Override public boolean addDatabase(String databaseName) throws DatasourceException, ExistsException { try { if (databaseExists(databaseName)) { throw new ExistsException("The database already exists in the datasource"); } MongoDatabase mongoDatabase = mongoClient.getDatabase(databaseName); String collectionName = "temp" + UUID.randomUUID(); mongoDatabase.createCollection(collectionName); mongoDatabase.getCollection(collectionName).drop(); return true; } catch (MongoException ex) { logger.error("An error occured while adding the database", ex); throw new DatasourceException("An error occured while adding the database"); } }
From source file:org.pac4j.mongo.test.tools.MongoServer.java
License:Apache License
public void start(final int port) { MongodStarter starter = MongodStarter.getDefaultInstance(); try {// w w w. j av a2s .c om IMongodConfig mongodConfig = new MongodConfigBuilder().version(Version.Main.PRODUCTION) .net(new Net(port, Network.localhostIsIPv6())).build(); mongodExecutable = starter.prepare(mongodConfig); MongodProcess mongod = mongodExecutable.start(); // populate final MongoClient mongo = new MongoClient("localhost", port); final MongoDatabase db = mongo.getDatabase("users"); db.createCollection("users"); final MongoCollection<Document> collection = db.getCollection("users"); final PasswordEncoder encoder = new BasicSaltedSha512PasswordEncoder(SALT); final String password = encoder.encode(PASSWORD); Map<String, Object> properties1 = new HashMap<>(); properties1.put(USERNAME, GOOD_USERNAME); properties1.put(PASSWORD, password); properties1.put(FIRSTNAME, FIRSTNAME_VALUE); collection.insertOne(new Document(properties1)); Map<String, Object> properties2 = new HashMap<>(); properties2.put(USERNAME, MULTIPLE_USERNAME); properties2.put(PASSWORD, password); collection.insertOne(new Document(properties2)); Map<String, Object> properties3 = new HashMap<>(); properties3.put(USERNAME, MULTIPLE_USERNAME); properties3.put(PASSWORD, password); collection.insertOne(new Document(properties3)); } catch (final IOException e) { throw new RuntimeException(e); } }
From source file:org.piotr.apollo.mongo.migration.AnswerMigration.java
public AnswerMigration() { System.out.println("Answer migration : start"); Boolean collExist = false;//from w w w .j a v a 2s . co m MongoDatabase db = mongo.getDatabase(); MongoIterable<String> collectionNames = db.listCollectionNames(); for (String colName : collectionNames) { if (colName.equals("answerCollection")) collExist = true; } if (!collExist) { System.out.println("Creating collection: answerCollection"); db.createCollection("answerCollection"); } System.out.println("Answer migration : stop"); }
From source file:org.piotr.apollo.mongo.migration.FinishedMigration.java
public FinishedMigration() { System.out.println("Finished migration : start"); Boolean collExist = false;//w w w .j ava 2 s . co m MongoDatabase db = mongo.getDatabase(); MongoIterable<String> collectionNames = db.listCollectionNames(); for (String colName : collectionNames) { if (colName.equals("finishedCollection")) collExist = true; } if (!collExist) { System.out.println("Creating collection: finishedCollection"); db.createCollection("finishedCollection"); } System.out.println("Finished migration : stop"); }
From source file:org.piotr.apollo.mongo.migration.LessonMigration.java
public LessonMigration() { System.out.println("Lesson migration : start"); Boolean collExist = false;//from w w w. j av a 2 s . c o m MongoDatabase db = mongo.getDatabase(); MongoIterable<String> collectionNames = db.listCollectionNames(); for (String colName : collectionNames) { if (colName.equals("lessonCollection")) collExist = true; } if (!collExist) { System.out.println("Creating collection: lessonCollection"); db.createCollection("lessonCollection"); } System.out.println("Lesson migration : stop"); }