我有以下问题:
我正在使用 MongoDB 3 的 Java 驱动程序。
在版本 2 中,可以执行 DB.collectionExists(name) 来检查所选数据库中是否存在集合。
在从 DB 切换到 MongoDatabase 的版本 3 中,此方法不再存在。
如何确定数据库中是否存在集合?我尝试使用 listCollectionNames() 遍历集合,但这似乎非常无效。
感谢您的帮助
最佳答案
另一种方法是使用 MongoIterable.into
函数将这些添加到可以调用 contains("collectionName")
的目标 ArrayList。
boolean collectionExists = client.getDatabase("dbName").listCollectionNames()
.into(new ArrayList<String>()).contains("collectionName")
关于java - MongoDB 3 Java检查集合是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31909247/