本文整理匯總了Java中com.mongodb.client.MongoDatabase.createCollection方法的典型用法代碼示例。如果您正苦於以下問題:Java MongoDatabase.createCollection方法的具體用法?Java MongoDatabase.createCollection怎麽用?Java MongoDatabase.createCollection使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.mongodb.client.MongoDatabase
的用法示例。
在下文中一共展示了MongoDatabase.createCollection方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: initCollections
import com.mongodb.client.MongoDatabase; //導入方法依賴的package包/類
private static void initCollections(MongoDatabase db) {
try {
db.createCollection("users");
db.createCollection("categories");
db.createCollection("feeds");
db.createCollection("licenses");
} catch (Exception e) {
Log.w(TAG, "[attemptCreateCollections] " + e.getMessage());
}
users = db.getCollection("users");
categories = db.getCollection("categories");
feeds = db.getCollection("feeds");
licenses = db.getCollection("licenses");
users.createIndex(
Indexes.ascending(User.USERNAME),
new IndexOptions().unique(true));
categories.createIndex(
Indexes.ascending(Category.OWNER, Category.KEY),
new IndexOptions().unique(true));
}
示例2: getCollectionList
import com.mongodb.client.MongoDatabase; //導入方法依賴的package包/類
public HashMap<String, MongoCollection> getCollectionList(MongoDatabase mongoDatabase) {
HashMap<String, MongoCollection> dbCollectionList = new HashMap<String, MongoCollection>();
String[] tableNameList = {athenaFeatureField.ERROR_MSG, athenaFeatureField.FLOW_REMOVED, athenaFeatureField.PACKET_IN,
athenaFeatureField.PORT_STATUS, athenaFeatureField.FLOW_STATS, athenaFeatureField.QUEUE_STATS,
athenaFeatureField.AGGREGATE_STATS, athenaFeatureField.TABLE_STATS, athenaFeatureField.PORT_STATS, "terabyte"};
String teraBytes = "terabyte";
for (String tableName : tableNameList) {
MongoCollection dbCollection = mongoDatabase.getCollection(tableName);
if (dbCollection == null) {
mongoDatabase.createCollection(tableName);
dbCollection = mongoDatabase.getCollection(tableName);
}
dbCollectionList.put(tableName, dbCollection);
}
return dbCollectionList;
}
示例3: getCollectionList
import com.mongodb.client.MongoDatabase; //導入方法依賴的package包/類
public HashMap<String, MongoCollection> getCollectionList(MongoDatabase mongoDatabase) {
HashMap<String, MongoCollection> dbCollectionList = new HashMap<String, MongoCollection>();
String[] tableNameList = {AthenaFeatureField.ERROR_MSG, AthenaFeatureField.FLOW_REMOVED,
AthenaFeatureField.PACKET_IN,
AthenaFeatureField.PORT_STATUS, AthenaFeatureField.FLOW_STATS, AthenaFeatureField.QUEUE_STATS,
AthenaFeatureField.AGGREGATE_STATS, AthenaFeatureField.TABLE_STATS, AthenaFeatureField.PORT_STATS};
for (String tableName : tableNameList) {
MongoCollection dbCollection = mongoDatabase.getCollection(tableName);
if (dbCollection == null) {
mongoDatabase.createCollection(tableName);
dbCollection = mongoDatabase.getCollection(tableName);
}
dbCollectionList.put(tableName, dbCollection);
}
return dbCollectionList;
}
示例4: createDbAndCollections
import com.mongodb.client.MongoDatabase; //導入方法依賴的package包/類
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);
}
IndexOptions indexOptions = new IndexOptions().unique(true)
.background(false).name(indexFieldName);
Bson keys = new Document(indexFieldName, Integer.valueOf(1));
mongoCollection.createIndex(keys, indexOptions);
}
示例5: should_return_that_the_collection_exists_in_the_database
import com.mongodb.client.MongoDatabase; //導入方法依賴的package包/類
@Test public void
should_return_that_the_collection_exists_in_the_database(){
String collectionName = "collection_registered_in_database";
MongoDatabase database = mongoEmbedded.getDatabase();
database.createCollection(collectionName);
MongoPersistentEntityIndex index = Mockito.mock(MongoPersistentEntityIndex.class);
MongoDataSource dataSource = Mockito.mock(MongoDataSource.class);
when(dataSource.getDataSource()).thenReturn(database);
MongoBasicOperations operation = new MongoBasicOperations(dataSource, index);
Assert.assertTrue("The 'exist' method returned the incorrect value", operation.exist(collectionName));
}
示例6: start
import com.mongodb.client.MongoDatabase; //導入方法依賴的package包/類
public void start(final int port) {
MongodStarter starter = MongodStarter.getDefaultInstance();
try {
IMongodConfig mongodConfig = new MongodConfigBuilder()
.version(Version.Main.PRODUCTION)
.net(new Net(port, Network.localhostIsIPv6()))
.build();
mongodExecutable = starter.prepare(mongodConfig);
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);
}
}