从MongoDB获取Java中的集合

从MongoDB获取Java中的集合,java,mongodb,Java,Mongodb,我有一个本地MongoDB实例在Windows10上通过shell运行。 大学为我们提供了一个Java项目来学习查询 现在,我有一个数据库(“imdb”),并希望从中获得两个集合(“电影”、“推特”) 问题是,一方面 List<String> test = mongo.getDatabaseNames(); System.out.println(test); //prints [admin,config,imdb,local] ... db = mongo.getDB("imdb")

我有一个本地MongoDB实例在Windows10上通过shell运行。 大学为我们提供了一个Java项目来学习查询

现在,我有一个数据库(“imdb”),并希望从中获得两个集合(“电影”、“推特”)

问题是,一方面

List<String> test = mongo.getDatabaseNames();
System.out.println(test); //prints [admin,config,imdb,local]
...
db = mongo.getDB("imdb");
System.out.println(db.getCollectionNames()); //prints []
返回com.mongodb.CommandFailureException,说明集合“imdb.movies”已存在

那么,如何确保Java实际“加载”集合呢? 澄清一下:我的目标是

System.out.println(db.getCollectionNames()); 

要打印[电影、推文]而不是[]

您使用的是哪个版本的mongodb shell

如果在3.0之前,它将不会为
db.getCollectionNames()
命令返回任何数据

参考:

您可以试试

Set<String> colls = db.getCollectionNames();

        for (String s : colls) {
            System.out.println(s);
        }
Set colls=db.getCollectionNames();
for(字符串s:colls){
系统输出打印项次;
}

ref:

从3.0版开始,您应该使用MongoDatabase.listCollectionNames()方法。

MongoDatabase db=client.getDatabase(dbName);
MongoIterable name=db.listCollectionNames();
您可以这样做:

//If you don't need connection's configures. i.e. your mongo is in you localhost at 127.0.0.1:27017
MongoClient cliente = new MongoClient(); //to connect into a mongo DB

MongoDatabase mongoDb = client.getDatabase("imdb");  //get database

MongoCollection<Document> robosCollection = mongoDb.getCollection("movies"); //get the name of the collection that you want

MongoCursor<Document> cursor =  robosCollection.find().cursor();//Mongo Cursor interface implementing the iterator protocol

    cursor.forEachRemaining(System.out::println); //print all documents in Collection using method reference
//如果不需要连接的配置。i、 e.您的mongo位于您的本地主机127.0.0.1:27017
MongoClient客户端=新的MongoClient()//连接到mongo DB的步骤
MongoDatabase mongoDb=client.getDatabase(“imdb”)//获取数据库
MongoCollection RoboCollection=mongoDb.getCollection(“电影”)//获取所需集合的名称
MongoCursor=robosCollection.find().cursor()//实现迭代器协议的Mongo游标接口
cursor.forEachRemaining(System.out::println)//使用方法引用打印集合中的所有文档

试图执行插入操作的代码,这就是您得到的已存在错误的原因,这篇文章可能会有所帮助,只需注释掉db.createCollection(“movies”,new BasicDBObject());你的错误会消失我不需要错误消失,createCollection只是检查电影“真实”是否存在于“imdb”中。根据大学提供的zip文件,它的3.6版表示,如果你使用
WiredTiger存储引擎
,它也适用。你能检查一下是否是这样吗?不知道如何检查,但在另一个shell中运行“show collections”会返回两个集合“movies”、“tweets”。因此,java等价物也应该起作用!?您可以在mongo shellName=wiredTiger、SupportsCommitteeDreads=true、readonly=false、persisten=true中尝试此命令
db.serverStatus().storageEngine
,这样看起来它确实使用了wiredTiger存储。但是show collections在Shell上不起作用我认为打印集合的每个字符串或整个集合没有什么区别。但是如果您尝试它,您可以看到db.getCollectionNames()是否返回任何内容。两者都应该检查它是否返回任何内容。不管怎样,你的建议没有打印任何内容
MongoDatabase db = client.getDatabase(dbName);
MongoIterable<String> names = db.listCollectionNames();
//If you don't need connection's configures. i.e. your mongo is in you localhost at 127.0.0.1:27017
MongoClient cliente = new MongoClient(); //to connect into a mongo DB

MongoDatabase mongoDb = client.getDatabase("imdb");  //get database

MongoCollection<Document> robosCollection = mongoDb.getCollection("movies"); //get the name of the collection that you want

MongoCursor<Document> cursor =  robosCollection.find().cursor();//Mongo Cursor interface implementing the iterator protocol

    cursor.forEachRemaining(System.out::println); //print all documents in Collection using method reference