Example usage for com.mongodb.client MongoDatabase getCollection

List of usage examples for com.mongodb.client MongoDatabase getCollection

Introduction

In this page you can find the example usage for com.mongodb.client MongoDatabase getCollection.

Prototype

<TDocument> MongoCollection<TDocument> getCollection(String collectionName, Class<TDocument> documentClass);

Source Link

Document

Gets a collection, with a specific default document class.

Usage

From source file:db.migrations.mongo.V1_1__Test_Mongo_Migration.java

License:Apache License

@Override
public void migrate(MongoClient client) throws Exception {
    MongoDatabase testDb = client.getDatabase("flyway_test");
    MongoCollection<BasicDBObject> testCollection = testDb.getCollection("testCollection", BasicDBObject.class);
    BasicDBObject testObj = new BasicDBObject().append("name", "Mr. Meeseeks").append("color", "blue");

    testCollection.insertOne(testObj);//from w  w w .ja va2  s.  c  o m
}

From source file:de.taimos.dvalin.mongo.MongoDBInit.java

License:Apache License

/**
 * init database with demo data//from  w  w w  . j av a  2s.c o  m
 */
@PostConstruct
public void initDatabase() {
    MongoDBInit.LOGGER.info("initializing MongoDB");
    String dbName = System.getProperty("mongodb.name");
    if (dbName == null) {
        throw new RuntimeException("Missing database name; Set system property 'mongodb.name'");
    }
    MongoDatabase db = this.mongo.getDatabase(dbName);

    try {
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        Resource[] resources = resolver.getResources("classpath*:mongodb/*.ndjson");
        MongoDBInit.LOGGER.info("Scanning for collection data");
        for (Resource res : resources) {
            String filename = res.getFilename();
            String collection = filename.substring(0, filename.length() - 7);
            MongoDBInit.LOGGER.info("Found collection file: " + collection);
            MongoCollection<DBObject> dbCollection = db.getCollection(collection, DBObject.class);
            try (Scanner scan = new Scanner(res.getInputStream(), "UTF-8")) {
                int lines = 0;
                while (scan.hasNextLine()) {
                    String json = scan.nextLine();
                    Object parse = JSON.parse(json);
                    if (parse instanceof DBObject) {
                        DBObject dbObject = (DBObject) parse;
                        dbCollection.insertOne(dbObject);
                    } else {
                        MongoDBInit.LOGGER.error("Invalid object found: " + parse);
                        throw new RuntimeException("Invalid object");
                    }
                    lines++;
                }
                MongoDBInit.LOGGER.info("Imported " + lines + " objects into collection " + collection);
            }
        }
    } catch (IOException e) {
        throw new RuntimeException("Error importing objects", e);
    }
}

From source file:it.terrinoni.m101j.App.java

public static void main(String[] args) {
    MongoClientOptions options = MongoClientOptions.builder().connectionsPerHost(100).build();
    MongoClient client = new MongoClient(new ServerAddress(), options);

    MongoDatabase db = client.getDatabase("test").withReadPreference(ReadPreference.secondary());

    MongoCollection<BsonDocument> coll = db.getCollection("test", BsonDocument.class);
}

From source file:org.apache.drill.exec.store.mongo.MongoRecordReader.java

License:Apache License

private void init(MongoSubScan.MongoSubScanSpec subScanSpec) {
    List<String> hosts = subScanSpec.getHosts();
    List<ServerAddress> addresses = Lists.newArrayList();
    for (String host : hosts) {
        addresses.add(new ServerAddress(host));
    }/*from  w w  w.  java2s.  com*/
    MongoClient client = plugin.getClient(addresses);
    MongoDatabase db = client.getDatabase(subScanSpec.getDbName());
    this.unionEnabled = fragmentContext.getOptions().getOption(ExecConstants.ENABLE_UNION_TYPE);
    collection = db.getCollection(subScanSpec.getCollectionName(), BsonDocument.class);
}