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

MongoCollection<Document> getCollection(String collectionName);

Source Link

Document

Gets a collection.

Usage

From source file:com.demo.mongodb.MongoDBSparkFreemarkerStyle.java

License:Apache License

public static void main(String[] args) {
    final Configuration configuration = new Configuration();
    configuration.setClassForTemplateLoading(MongoDBSparkFreemarkerStyle.class, "/");

    MongoClient client = new MongoClient();

    MongoDatabase database = client.getDatabase("m101");
    final MongoCollection<Document> collection = database.getCollection("funnynumbers");

    Spark.get(new Route("/") {
        @Override/*from  ww w .j a  va  2  s  .co m*/
        public Object handle(final Request request, final Response response) {
            StringWriter writer = new StringWriter();
            try {
                Template template = configuration.getTemplate("answer.ftl");

                // Not necessary yet to understand this.  It's just to prove that you
                // are able to run a command on a mongod server
                List<Document> results = collection.aggregate(asList(
                        new Document("$group",
                                new Document("_id", "$value").append("count", new Document("$sum", 1))),
                        new Document("$match", new Document("count", new Document("$lte", 2))),
                        new Document("$sort", new Document("_id", 1)))).into(new ArrayList<Document>());

                int answer = 0;
                for (Document cur : results) {
                    answer += (Double) cur.get("_id");
                }

                Map<String, String> answerMap = new HashMap<String, String>();
                answerMap.put("answer", Integer.toString(answer));

                template.process(answerMap, writer);
            } catch (Exception e) {
                e.printStackTrace();
                halt(500);
            }
            return writer;
        }
    });
}

From source file:com.dilmus.dilshad.scabi.db.DTable.java

License:Open Source License

public DTable(DDB ddb, String tableName) throws DScabiException {
    m_ddb = null;/*w  ww. j  a v a 2s.  c  o m*/
    m_table = null;
    m_tableName = null;
    m_firstTime = true;
    m_fieldNames = null;

    MongoDatabase db = ddb.getDatabase();
    if (false == ddb.tableExists(tableName)) {
        throw new DScabiException("Table name doesn't exist : " + tableName, "DBT.DBT.1");
    }
    m_table = db.getCollection(tableName);
    m_tableName = tableName;
    m_ddb = ddb;

    m_dcursor = new DResultSet();

}

From source file:com.dilmus.dilshad.scabi.db.DTable.java

License:Open Source License

public int setTableName(String tableName) throws DScabiException {
    m_tableName = null;//from   w w w  . ja  v  a 2  s.c om
    m_firstTime = true;
    m_fieldNames = null;
    m_table = null;

    MongoDatabase db = m_ddb.getDatabase();
    if (false == m_ddb.tableExists(tableName)) {
        throw new DScabiException("Table name doesn't exist : " + tableName, "DBT.STN.1");
    }
    m_table = db.getCollection(tableName);
    m_tableName = tableName;
    return 0;
}

From source file:com.eclipsesource.connect.persistence.MongoStorage.java

License:Open Source License

@Override
public void store(String place, Object object, Object... objects) {
    validateArguments(place, object);/*from  w w  w.  ja va2 s  . c  om*/
    MongoDatabase db = factory.getDB();
    MongoCollection<Document> collection = db.getCollection(place);
    List<Document> documents = createDocuments(object, objects);
    documents.forEach(document -> {
        collection.updateOne(eq(KEY_ID, document.get(KEY_ID)), new Document("$set", document), UPDATE_OPTIONS);
    });
    notifyObservers(place, object, objects);
}

From source file:com.eclipsesource.connect.persistence.MongoStorage.java

License:Open Source License

@Override
public void delete(String place, Object object, Object... objects) {
    validateArguments(place, object);/*www  .j  av  a 2s  .co m*/
    MongoDatabase db = factory.getDB();
    MongoCollection<Document> collection = db.getCollection(place);
    List<Document> documents = createDocuments(object, objects);
    documents.forEach(document -> collection.deleteOne(new Document(KEY_ID, document.get(KEY_ID))));
}

From source file:com.eclipsesource.connect.persistence.MongoStorage.java

License:Open Source License

@Override
public void delete(Query<?> query) {
    checkArgument(query != null, "Query must not be null");
    MongoDatabase db = factory.getDB();
    MongoCollection<Document> collection = db.getCollection(query.getPlace());
    Document document = createDocument(query);
    collection.deleteMany(document);/*from w  w  w .  ja  v a  2s  .  c  om*/
}

From source file:com.eclipsesource.connect.persistence.MongoStorage.java

License:Open Source License

@Override
public <T> List<T> findAll(Query<T> query) {
    checkArgument(query != null, "Query must not be null");
    MongoDatabase db = factory.getDB();
    MongoCollection<Document> collection = db.getCollection(query.getPlace());
    List<T> result = new ArrayList<>();
    for (Document document : prepareIterable(query, collection.find(createDocument(query)))) {
        result.add(deserializer.deserialize(document.toJson(), query.getType()));
    }/*from   www  . j  a  v a  2 s  . c o m*/
    return result;
}

From source file:com.eclipsesource.connect.persistence.MongoStorage.java

License:Open Source License

@Override
public long count(Query<?> query) {
    checkArgument(query != null, "Query must not be null");
    MongoDatabase db = factory.getDB();
    MongoCollection<Document> collection = db.getCollection(query.getPlace());
    return collection.count(createDocument(query));
}

From source file:com.enlightendev.mongodb.MongoDBSparkFreemarkerStyle.java

License:Apache License

public static void main(String[] args) {
    final Configuration configuration = new Configuration();
    configuration.setClassForTemplateLoading(MongoDBSparkFreemarkerStyle.class, "/freemarker");

    MongoClient client = new MongoClient();

    MongoDatabase database = client.getDatabase("m101");
    final MongoCollection<Document> collection = database.getCollection("funnynumbers");

    Spark.get(new Route("/") {
        @Override/*from w  ww  . j  a  v a2s  .  c om*/
        public Object handle(final Request request, final Response response) {
            StringWriter writer = new StringWriter();
            try {
                Template template = configuration.getTemplate("answer.ftl");

                // Not necessary yet to understand this.  It's just to prove that you
                // are able to run a command on a mongod server
                List<Document> results = collection.aggregate(asList(
                        new Document("$group",
                                new Document("_id", "$value").append("count", new Document("$sum", 1))),
                        new Document("$match", new Document("count", new Document("$lte", 2))),
                        new Document("$sort", new Document("_id", 1)))).into(new ArrayList<Document>());

                int answer = 0;
                for (Document cur : results) {
                    answer += (Double) cur.get("_id");
                }

                Map<String, String> answerMap = new HashMap<String, String>();
                answerMap.put("answer", Integer.toString(answer));

                template.process(answerMap, writer);
            } catch (Exception e) {
                e.printStackTrace();
                halt(500);
            }
            return writer;
        }
    });
}

From source file:com.facebook.presto.mongodb.MongoSession.java

License:Apache License

private Document getTableMetadata(SchemaTableName schemaTableName) throws TableNotFoundException {
    String schemaName = schemaTableName.getSchemaName();
    String tableName = schemaTableName.getTableName();

    MongoDatabase db = client.getDatabase(schemaName);
    MongoCollection<Document> schema = db.getCollection(schemaCollection);

    Document doc = schema.find(new Document(TABLE_NAME_KEY, tableName)).first();

    if (doc == null) {
        if (!collectionExists(db, tableName)) {
            throw new TableNotFoundException(schemaTableName);
        } else {//from   ww w  .j  ava 2 s .c  om
            Document metadata = new Document(TABLE_NAME_KEY, tableName);
            metadata.append(FIELDS_KEY, guessTableFields(schemaTableName));

            schema.createIndex(new Document(TABLE_NAME_KEY, 1), new IndexOptions().unique(true));
            schema.insertOne(metadata);

            return metadata;
        }
    }

    return doc;
}