Java Code Examples for com.mongodb.DB.createCollection()

The following are Jave code examples for showing how to use createCollection() of the com.mongodb.DB class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: mongodb-broker   File: MongoAdminService.java   View Source Code Vote up 6 votes
public DB createDatabase(String databaseName) throws MongoServiceException {
		try {
			DB db = client.getDB(databaseName);
			
			// save into a collection to force DB creation.
			DBCollection col = db.createCollection("foo", null);
			BasicDBObject obj = new BasicDBObject();
			obj.put("foo", "bar");
			col.insert(obj);
			// drop the collection so the db is empty
//			col.drop();
			
			return db; 
		} catch (MongoException e) {
			// try to clean up and fail
			try {
				deleteDatabase(databaseName);
			} catch (MongoServiceException ignore) {}
			throw handleException(e);
		}
	}
 
Example 2
Project: hadoop-mini-clusters   File: MongodbLocalServerIntegrationTest.java   View Source Code Vote up 6 votes
@Test
public void testMongodbLocalServer() throws Exception {
    MongoClient mongo = new MongoClient(mongodbLocalServer.getIp(), mongodbLocalServer.getPort());

    DB db = mongo.getDB(propertyParser.getProperty(ConfigVars.MONGO_DATABASE_NAME_KEY));
    DBCollection col = db.createCollection(propertyParser.getProperty(ConfigVars.MONGO_COLLECTION_NAME_KEY),
            new BasicDBObject());
    
    col.save(new BasicDBObject("testDoc", new Date()));
    LOG.info("MONGODB: Number of items in collection: {}", col.count());
    assertEquals(1, col.count());
    
    DBCursor cursor = col.find();
    while(cursor.hasNext()) {
        LOG.info("MONGODB: Document output: {}", cursor.next());
    }
    cursor.close();
}
 
Example 3
Project: xxl-incubator   File: MongoDBUtil.java   View Source Code Vote up 5 votes
/**
 * 新增DBCollection对象 = 表
 * @param dbName
 * @param collName
 * @param options
 * @return
 */
public static DBCollection addCollection(String dbName, String collName, DBObject options) {
	DB db = getDB(dbName);
	if (db != null) {
		DBCollection collection = db.createCollection(collName, options);
		return collection;
	}
    return null;
}
 
Example 4
Project: spring-cloud-stream-app-starters   File: MongodbSourceApplicationTests.java   View Source Code Vote up 5 votes
@Before
public void setUp() {
	DB db = mongo.getDB("test");
	DBCollection col = db.createCollection("testing", new BasicDBObject());
	col.save(new BasicDBObject("greeting", "hello"));
	col.save(new BasicDBObject("greeting", "hola"));
}