public boolean collectionExists(MongoDatabase db, String collectionName) { for (String name : db.listCollectionNames()) { if (name.equalsIgnoreCase(collectionName)) { return true; } } return false; }
public Boolean doInDB(MongoDatabase db) throws MongoException, DataAccessException { for (String name : db.listCollectionNames()) { if (name.equals(collectionName)) { return true; } } return false; } });
public Set<String> getAllTables(String schema) throws SchemaNotFoundException { ImmutableSet.Builder<String> builder = ImmutableSet.builder(); builder.addAll(ImmutableList.copyOf(client.getDatabase(schema).listCollectionNames()).stream() .filter(name -> !name.equals(schemaCollection)) .filter(name -> !SYSTEM_TABLES.contains(name)) .collect(toSet())); builder.addAll(getTableMetadataNames(schema)); return builder.build(); }
public Set<String> doInDB(MongoDatabase db) throws MongoException, DataAccessException { Set<String> result = new LinkedHashSet<>(); for (String name : db.listCollectionNames()) { result.add(name); } return result; } });
/** * Perform the given operation on each of the collection names in the named database. * * @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); }
/** * Perform the given operation on the named collection in the named database, if the database and collection both exist. * * @param client the MongoDB client; may not be null * @param dbName the name of the database; may not be null * @param collectionName the name of the collection; may not be null * @param collectionOperation the operation to perform; may not be null */ public static void onCollection(MongoClient client, String dbName, String collectionName, Consumer<MongoCollection<Document>> collectionOperation) { onDatabase(client, dbName, db -> { if (contains(db.listCollectionNames(), collectionName)) { collectionOperation.accept(db.getCollection(collectionName)); } }); }
public static void cleanDatabase(MongoPrimary primary, String dbName) { primary.execute("clean-db", mongo -> { MongoDatabase db1 = mongo.getDatabase(dbName); db1.listCollectionNames().forEach((Consumer<String>) ((String x) -> { logger.info("Removing collection '{}' from database '{}'", x, dbName); db1.getCollection(x).drop(); })); }); } }
public Boolean doInDB(MongoDatabase db) throws MongoException, DataAccessException { for (String name : db.listCollectionNames()) { if (name.equals(collectionName)) { return true; } } return false; } });
public Set<String> doInDB(MongoDatabase db) throws MongoException, DataAccessException { Set<String> result = new LinkedHashSet<>(); for (String name : db.listCollectionNames()) { result.add(name); } return result; } });
private MongoDatabase createMongoDatabaseAndItsCollections(MongoDatabase mongoDatabase, com.mongodb.client.MongoDatabase database) { MongoIterable<String> collectionNames = database.listCollectionNames(); for (String collectionName : collectionNames) { mongoDatabase.addCollection(new MongoCollection(collectionName, mongoDatabase)); } return mongoDatabase; }
public void connect(final ServerConfiguration configuration) { Task task = mongoClient -> { String userDatabase = configuration.getUserDatabase(); String databaseName = StringUtils.isNotEmpty(userDatabase) ? userDatabase : "test"; mongoClient.getDatabase(databaseName).listCollectionNames().first(); }; executeTask(configuration, task); }
@Test public void removeCollection() { mongoManager.removeCollection(ServerConfiguration.byDefault(), createMongoCollectionForTest()); MongoDatabase testDatabase = new MongoClient("localhost:27017").getDatabase("test"); ArrayList<String> collections = testDatabase .listCollectionNames() .into(new ArrayList<>()); assertThat(collections.contains("people")).isFalse(); }
private boolean isCollectionExists(String collectionName) { boolean result = false; MongoIterable<String> allCollections = db.listCollectionNames(); for ( String collection : allCollections ) { if (collection.equals(collectionName)) { result = true; break; } } return result; }
/** {@inheritDoc} */ @Override public void createSchema() { if (!mongoClient.getDatabase(dbName) .listCollectionNames() .into(new HashSet<String>()) .contains(collectionName)) { mongoClient.getDatabase(dbName).createCollection(collectionName); } propertiesCollection = mongoClient.getDatabase(dbName).getCollection(collectionName); }
/** {@inheritDoc} */ @Override public void createSchema() { if (!mongoClient.getDatabase(dbName) .listCollectionNames() .into(new HashSet<String>()) .contains(collectionName)) { mongoClient.getDatabase(dbName).createCollection(collectionName); } eventsCollection = mongoClient.getDatabase(dbName).getCollection(collectionName); }
private void deleteAllElements(MongoDatabase mongoDb) { final MongoIterable<String> listCollectionNames = mongoDb.listCollectionNames(); for (String collectionName : listCollectionNames) { if (isNotASystemCollection(collectionName)) { LOGGER.debug("Dropping Collection {}.", collectionName); MongoCollection dbCollection = mongoDb.getCollection(collectionName); // Delete ALL, No DROP dbCollection.deleteMany(new Document()); } } }
@NotNull @Override public MongoIterable<String> listCollectionNames() { return db.listCollectionNames(); }
@Test public void testCreateCollection() throws Exception { String newCollectionName = "some-collection"; assertThat(toArray(db.listCollectionNames())).doesNotContain(newCollectionName); db.createCollection(newCollectionName, new CreateCollectionOptions()); assertThat(toArray(db.listCollectionNames())).contains(newCollectionName); }
@Test public void testGetCollectionNames() throws Exception { getCollection("foo").insertOne(json("")); getCollection("bar").insertOne(json("")); List<String> collectionNames = toArray(db.listCollectionNames()); assertThat(collectionNames).containsExactlyInAnyOrder("foo", "bar"); }
@Test public void testRenameCollection() throws Exception { collection.insertOne(json("_id: 1")); collection.insertOne(json("_id: 2")); collection.insertOne(json("_id: 3")); collection.renameCollection(new MongoNamespace(collection.getNamespace().getDatabaseName(), "other-collection-name")); Collection<String> collectionNames = toArray(db.listCollectionNames()); assertThat(collectionNames).containsExactly("other-collection-name"); assertThat(getCollection("other-collection-name").countDocuments()).isEqualTo(3); }