Java Code Examples for com.mongodb.client.MongoDatabase#listCollectionNames()

The following examples show how to use com.mongodb.client.MongoDatabase#listCollectionNames() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LumongoIndexManager.java    From lumongo with Apache License 2.0 6 votes vote down vote up
public List<String> getIndexNames() {
	globalLock.writeLock().lock();

	try {
		ArrayList<String> indexNames = new ArrayList<>();
		log.info("Searching database <" + mongoConfig.getDatabaseName() + "> for indexes");
		MongoDatabase db = mongo.getDatabase(mongoConfig.getDatabaseName());
		MongoIterable<String> allCollections = db.listCollectionNames();

		for (String collection : allCollections) {
			if (collection.endsWith(LumongoIndex.CONFIG_SUFFIX)) {
				String indexName = collection.substring(0, collection.length() - LumongoIndex.CONFIG_SUFFIX.length());
				indexNames.add(indexName);
			}

		}

		return indexNames;
	}
	finally {
		globalLock.writeLock().unlock();
	}
}
 
Example 2
Source File: CmdIdxAccessStats.java    From mongodb-slow-operations-profiler with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public TableDto runCommand(ProfilingReader profilingReader, MongoDbAccessor mongoDbAccessor) {
    final TableDto result = new TableDto();
    final MongoDatabase database = mongoDbAccessor.getMongoDatabase(profilingReader.getDatabase());
    final MongoIterable<String> collNames = database.listCollectionNames();

    if(collNames != null){
        for(String collName : collNames){
            if(!"system.profile".equals(collName)) {
                final TableDto collStats = getIndexStats(database.getCollection(collName), profilingReader.getProfiledServerDto().getLabel());
                result.addRows(collStats);
            }
        }
    }
    return result;
}
 
Example 3
Source File: MongoSession.java    From presto with Apache License 2.0 5 votes vote down vote up
public boolean collectionExists(MongoDatabase db, String collectionName)
{
    for (String name : db.listCollectionNames()) {
        if (name.equalsIgnoreCase(collectionName)) {
            return true;
        }
    }
    return false;
}
 
Example 4
Source File: WebController.java    From Mongodb-WeAdmin with Apache License 2.0 5 votes vote down vote up
/**
   * 数据库对应的数据集合列表
   * @param dbName
   * @return
   */
  @ResponseBody
  @RequestMapping("/db")
  public Res db(String dbName) {

  	if(StringUtils.isEmpty(dbName)){
  		return Res.error("dbName参数不能为空");
  	}
  	if("undefined".equals(dbName)){
	return Res.error("请关闭所有的iframe后在执行F5");
}
      MongoDatabase mogo = mongoSdkBase.getMongoDb(dbName);
      //获取所有集合的名称
      MongoIterable<String> collectionNames = mogo.listCollectionNames();
      MongoCursor<String> i = collectionNames.iterator();
      List<JSONObject> listNames = new ArrayList<JSONObject>();
      while (i.hasNext()) {
          String tableName = i.next();
	if(!Arrays.asList(TAVLEARR).contains(tableName)) {
		JSONObject t = new JSONObject();
		t.put("tableName", tableName);
		BasicDBObject obj = mongoSdkBase.getStats(dbName, tableName);
		t.put("size", ByteConvKbUtils.getPrintSize(obj.getInt("size")));
		listNames.add(t);
	}

      }
      return Res.ok().put("listNames", listNames);
  }
 
Example 5
Source File: DataSetComparator.java    From jpa-unit with Apache License 2.0 5 votes vote down vote up
private void shouldBeEmpty(final MongoDatabase connection, final AssertionErrorCollector errorCollector) {
    for (final String collectionName : connection.listCollectionNames()) {
        final long rowCount = connection.getCollection(collectionName).count();
        if (rowCount != 0) {
            errorCollector.collect(collectionName + " was expected to be empty, but has <" + rowCount + "> entries.");
        }
    }
}
 
Example 6
Source File: MongoUtil.java    From render with GNU General Public License v2.0 5 votes vote down vote up
public static boolean exists(final MongoDatabase database,
                             final String collectionName) {
    for (final String name : database.listCollectionNames()) {
        if (name.equals(collectionName)) {
            return true;
        }
    }
    return false;
}
 
Example 7
Source File: MongoDocumentDb.java    From mdw with Apache License 2.0 4 votes vote down vote up
@Override
public void initializeDbClient() {
    if (mongoClient == null) {
        MongoClientOptions.Builder options = MongoClientOptions.builder();

        if (dbHost.startsWith("mongodb") && dbHost.contains("://")) {
            boolean needMaxConnections = true;
            boolean needReadConcern = true;
            boolean needWriteConcern = true;
            boolean needReadPreference = true;

            int index = dbHost.indexOf("?");
            if (index > 0 && dbHost.length() > index+2) { // Means URI has options specified
                for (String entry : dbHost.substring(index+1).split("[&;]")) {
                    String[] option = entry.split("=");
                    if (option.length == 2 && option[1] != null) {
                        switch (option[0]) {
                            case MAX_POOL_SIZE:
                                needMaxConnections = false;
                                break;
                            case READ_CONCERN:
                                needReadConcern = false;
                                break;
                            case WRITE_CONCERN:
                                needWriteConcern = false;
                                break;
                            case READ_PREFERENCE:
                                needReadPreference = false;
                                break;
                            default:
                                break;
                        }
                    }
                }
            }
            // Apply default option needed for replica sets whenever not specified in URI
            if (needMaxConnections)
                options = options.connectionsPerHost(maxConnections);
            if (needReadConcern)
                options = options.readConcern(ReadConcern.MAJORITY);
            if (needWriteConcern)
                options = options.writeConcern(WriteConcern.MAJORITY);
            if (needReadPreference)
                options = options.readPreference(ReadPreference.primary());

            mongoClient = new MongoClient(new MongoClientURI(dbHost, options));
            dbPort = 0;
        }
        else {
            mongoClient = new MongoClient(new ServerAddress(dbHost, dbPort), options.connectionsPerHost(maxConnections).writeConcern(WriteConcern.ACKNOWLEDGED).build());
        }

        MongoDatabase mongoDb = MongoDocumentDb.getMongoDb();
        if (mongoDb != null)
            for (String name : mongoDb.listCollectionNames()) {
                if (name.startsWith(MDW_PREFIX)) {
                    boolean needsIndex = true;
                    for (Document doc : mongoDb.getCollection(name).listIndexes()) {
                        if ("document_id_1".equals(doc.getString("name"))) {
                            needsIndex = false;
                            collectionDocIdIndexed.putIfAbsent(name, true);
                        }
                    }
                    if (needsIndex)
                        createMongoDocIdIndex(name);
                }
            }
        LoggerUtil.getStandardLogger().info(mongoClient.getMongoClientOptions().toString());
    }
}
 
Example 8
Source File: MongoContext.java    From immutables with Apache License 2.0 4 votes vote down vote up
/**
 * Drops all collections from a mongo database
 */
private static void clearDatabase(MongoDatabase database) {
  for (String name: database.listCollectionNames()) {
    database.getCollection(name).drop();
  }
}