0

I followed several different tutorials for his but everytime more or less nothing happens. Since I had problems with "ClassNotFoundException" I used the mongodb driver jar file suggested in this question: Stackoverflow Topic

I have a very simple Java Project with a Class Test running the main method to connect to my database "local" and to the collection "Countries" according to several tutorials, the data should be inserted as defined in the code. But when I check the collection in command line or Studio 3T it is still empty. There are some unused imports due to several tests before.

import org.bson.Document;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.Mongo;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;

public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        try {


        MongoClient connection = new MongoClient("localhost", 27017);
        DB db = connection.getDB("local");
        DBCollection coll = db.getCollection("Countries");


         BasicDBObject doc = new BasicDBObject("title", "MongoDB").
                 append("name","Germany" ).
                 append("population", "82 000 000");

                 coll.insert(doc);
        System.out.print("Test");
        }

        catch(Exception e) {


            System.out.print(e);
            System.out.print("Test");

        }






    }

}

The output is the following:

Usage : [--bucket bucketname] action
  where  action is one of:
      list                      : lists all files in the store
      put filename              : puts the file filename into the store
      get filename1 filename2   : gets filename1 from store and sends to filename2
      md5 filename              : does an md5 hash on a file in the db (for testing)

I dont get why the insert is not working and in addition why the System.out.print methods are not showing up. The getDB method is also cancelled in eclipse saying "The method getDB(String) from the type Mongo is deprecated" that i do not really understand. I hope someone can help me to get the code working. Mongod.exe is running in the background.

Julian Herbold
  • 537
  • 9
  • 20
  • 2
    You should not be writing data to the local database. Data in the local database is not replicated (https://docs.mongodb.com/manual/reference/local-database/#overview) and could result in unexpected behavior if your deployment is deployed as a replica set. – Adam Harrison Jun 13 '17 at 19:17
  • thanks I will have a look at it later ! – Julian Herbold Jun 13 '17 at 20:10
  • I changed the database to "mydb" (own database) but still nothing happens – Julian Herbold Jun 14 '17 at 10:09

0 Answers0