Example usage for com.mongodb.client MongoDatabase listCollectionNames

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

Introduction

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

Prototype

MongoIterable<String> listCollectionNames();

Source Link

Document

Gets the names of all the collections in this database.

Usage

From source file:anuncius.singleton.MongoHandler.java

public ArrayList<String> getAllCollectionsNames(String database) {
    MongoDatabase db = mongo.getDatabase(database);
    MongoIterable<String> names = db.listCollectionNames();

    ArrayList<String> namesList = new ArrayList<>();
    for (String name : names) {
        namesList.add(name);/*  w w w .j a  va 2  s .  co m*/
    }
    return namesList;
}

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

License:Apache License

public boolean collectionExists(MongoDatabase db, String collectionName) {
    for (String name : db.listCollectionNames()) {
        if (name.equalsIgnoreCase(collectionName)) {
            return true;
        }// w ww  .j  a v  a  2 s  .  c  o  m
    }
    return false;
}

From source file:com.jaeksoft.searchlib.crawler.database.DatabaseCrawlMongoDb.java

License:Open Source License

@Override
public String test() throws Exception {
    URI uri = new URI(getUrl());
    StringBuilder sb = new StringBuilder();
    if (!"mongodb".equals(uri.getScheme()))
        throw new SearchLibException(
                "Wrong scheme: " + uri.getScheme() + ". The URL should start with: mongodb://");
    MongoClient mongoClient = null;// w  w w.j  av a 2  s .  c  o  m
    try {
        mongoClient = getMongoClient();
        sb.append("Connection established.");
        sb.append(StringUtils.LF);
        if (!StringUtils.isEmpty(databaseName)) {
            MongoDatabase db = mongoClient.getDatabase(databaseName);
            if (db == null)
                throw new SearchLibException("Database not found: " + databaseName);
            MongoIterable<String> collections = db.listCollectionNames();
            if (collections == null)
                throw new SearchLibException("No collection found.");
            sb.append("Collections found:");
            sb.append(StringUtils.LF);
            for (String collection : collections) {
                sb.append(collection);
                sb.append(StringUtils.LF);
            }
            if (!StringUtils.isEmpty(collectionName)) {
                MongoCollection<?> dbCollection = db.getCollection(collectionName);
                if (dbCollection == null)
                    throw new SearchLibException("Collection " + collectionName + " not found.");
                sb.append(
                        "Collection " + collectionName + " contains " + dbCollection.count() + " document(s).");
                sb.append(StringUtils.LF);
                if (!StringUtils.isEmpty(criteria)) {
                    long count = dbCollection.count(getCriteriaObject());
                    sb.append("Query returns " + count + " document(s).");
                    sb.append(StringUtils.LF);
                }
            }
        }
    } finally {
        if (mongoClient != null)
            mongoClient.close();
    }
    return sb.toString();
}

From source file:com.px100systems.data.plugin.storage.mongo.MongoDatabaseStorage.java

License:Open Source License

public Map<String, List<String>> getSchema(boolean reset) {
    MongoDatabase db = mongoClient.getDatabase(databaseName);

    Map<String, List<String>> result = new HashMap<>();

    MongoCursor<String> cursor = db.listCollectionNames().iterator();
    try {//  w  w w.java2s  .  c  om
        while (cursor.hasNext()) {
            String name = cursor.next();
            if (!name.startsWith("system."))
                if (reset)
                    db.getCollection(name).drop();
                else
                    result.put(name, new ArrayList<String>());
        }
    } finally {
        cursor.close();
    }

    for (Map.Entry<String, List<String>> e : result.entrySet()) {
        MongoCursor<Document> idxCursor = db.getCollection(e.getKey()).listIndexes().iterator();
        try {
            while (idxCursor.hasNext()) {
                Document idx = idxCursor.next();
                String indexName = idx.get("name", String.class);
                if (CompoundIndexDescriptor.isCompondIndexName(indexName))
                    e.getValue().add(indexName);
                else {
                    String name = idx.get("key", Document.class).keySet().iterator().next();
                    if (!name.startsWith("_"))
                        e.getValue().add(name);
                }
            }
        } finally {
            idxCursor.close();
        }
    }

    return result;
}

From source file:com.speedment.connector.mongodb.MongoDbmsHandler.java

License:Open Source License

private Stream<String> collectionNames(MongoDatabase mongo) {
    return StreamSupport.stream(mongo.listCollectionNames().spliterator(), EXECUTE_IN_PARALLEL);
}

From source file:de.dfki.mmf.examples.craft_task_example.MongoDBWorldDescription.java

License:Open Source License

public MongoDatabase clearDatabase(String dbName) {
    MongoClient mongoClient = new MongoClient();
    MongoDatabase db = mongoClient.getDatabase(dbName);
    MongoIterable<String> collectionNames = db.listCollectionNames();
    for (String name : collectionNames) {
        db.getCollection(name).drop();//  www.  ja v  a  2  s .  c o  m
    }
    return db;
}

From source file:de.dfki.mmf.examples.craft_task_example.MongoDBWorldDescription.java

License:Open Source License

public void printAllDatabaseEntries(MongoDatabase db) {
    MongoIterable<String> collectionNames = db.listCollectionNames();
    for (String name : collectionNames) {
        FindIterable<Document> iterable = db.getCollection(name).find();
        iterable.forEach(new Block<Document>() {
            public void apply(final Document document) {
                System.out.println(document.toJson());
            }//from   w ww  .  j  a v  a2  s  .c o m
        });
        System.out.println("----------------------");
    }
}

From source file:de.dfki.mmf.input.worldmodel.WorldModelFactory.java

License:Open Source License

/**
 *
 * @param saliencyAnnotation/*from  w ww . ja v  a  2 s. c  om*/
 * @param robotModel
 * @param databaseName
 * Retrieve object properties from a MongoDB database
 */
private void mongoDBModelRetrieval(JsonObject saliencyAnnotation, RobotModel robotModel, String databaseName) {
    MongoClient mongoClient = new MongoClient();
    MongoDatabase db = mongoClient.getDatabase(databaseName);
    MongoIterable<String> collectionNames = db.listCollectionNames();
    ArrayList<JsonObject> worldProperties = new ArrayList<>();
    for (String name : collectionNames) {
        FindIterable<Document> iterable = db.getCollection(name).find();
        iterable.forEach(new Block<Document>() {
            public void apply(final Document document) {
                JsonObject jsonObject = (new JsonParser()).parse(document.toJson()).getAsJsonObject();
                worldProperties.add(jsonObject);
            }
        });
    }
    postProcessWorldModel(worldProperties, saliencyAnnotation, robotModel);

}

From source file:info.bunji.mongodb.synces.CollectionExtractor.java

License:Apache License

/**
 **********************************//from   ww  w  .j  a va 2 s.c  om
 * ??????.
 * <br>
 * ????????
 * @return ????
 **********************************
 */
private Set<String> getTargetColectionList(MongoDatabase db) {
    Set<String> collectionSet = config.getImportCollections();
    if (collectionSet.isEmpty()) {
        // ??????(???)
        MongoCursor<String> it = db.listCollectionNames().iterator();
        while (it.hasNext()) {
            String name = it.next();
            if (!name.startsWith("system.")) {
                collectionSet.add(name);
            }
        }
    }
    return collectionSet;
}

From source file:io.debezium.connector.mongodb.MongoUtil.java

License:Apache License

/**
 * Perform the given operation on each of the collection names in the named database.
 * //from w w w  .  j  av  a 2 s .  c o  m
 * @param client the MongoDB client; may not be null
 * @param databaseName the name of the database; may not be null
 * @param operation the operation to perform; may not be null
 */
public static void forEachCollectionNameInDatabase(MongoClient client, String databaseName,
        Consumer<String> operation) {
    MongoDatabase db = client.getDatabase(databaseName);
    forEach(db.listCollectionNames(), operation);
}