0

I have a local MongoDB instance running via shell on windows 10. University provided a Java Project for us to learn about queries.

Now, I have a database ("imdb") and want to get two collections from it ("movies","tweets").

The problem is, one the one hand

List<String> test = mongo.getDatabaseNames();
System.out.println(test); //prints [admin,config,imdb,local]
...
db = mongo.getDB("imdb");
System.out.println(db.getCollectionNames()); //prints []

There seem to be no collections on imdb but

db.createCollection("movies", new BasicDBObject());

Returns a com.mongodb.CommandFailureException, stating that a collection 'imdb.movies' already exists.

So how do I ensure that Java actually "loads" the Collections? For Clarification: My goal is to have

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

to print [movies,tweets] instead of []

2
  • The code trying to do an insert that is why you get already exists error, this post here might help, just comment out the line db.createCollection("movies", new BasicDBObject()); your error will go away Commented Jun 19, 2018 at 11:11
  • I don't need the Error to go away, createCollection was just do check if movies "truely" doesnt exist in "imdb". Commented Jun 19, 2018 at 11:17

4 Answers 4

1

You could try

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

        for (String s : colls) {
            System.out.println(s);
        }

ref: code examples

Sign up to request clarification or add additional context in comments.

3 Comments

I think there is little difference in printing every String of a Set or the Set in whole.
But if you try it you can see if db.getCollectionNames() is returning anything.
Both should check if it returns anything. Anyhow, your suggestion doesn't print anything
0

Which version of mongodb shell you're using

If it's before 3.0, it will return no data for db.getCollectionNames() command

ref: mongodb docs

6 Comments

According to the zip file provided by university its Version 3.6
And here said it's also apply if you're using WiredTiger storage engine. Can you check if it's the case.
Not quiet sure how to check that, but running "show collections" in another shell returns both collections "movies","tweets". Therefore the java equivalent should work too!?
Can you try this command db.serverStatus().storageEngine in the mongo shell
Name = wiredTiger, supportscommittedreads = true, readonly = false, persisten = true So it seems like it does use wiredTiger storage. But show collections works nontheless on the shell
|
0

Since version 3.0 you should use MongoDatabase.listCollectionNames() method. http://mongodb.github.io/mongo-java-driver/3.8/driver/tutorials/databases-collections/#get-a-list-of-collections

MongoDatabase db = client.getDatabase(dbName);
MongoIterable<String> names = db.listCollectionNames();

Comments

0

You can do like this:

//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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.