0

This is my first time trying to use the MongoDB java driver, so I'm just trying out the examples. I've added the driver jar to the libraries, and all the imports works fine. However, whenever I try to write anything else, like MongoClient mongoClient = new MongoClient(); I get the error Syntax error on tokens, delete these tokens. In fact, it's not just mongo-specific. Writing anything creates this error.

I apologize if the answer is really obvious :/

ritesh
  • 907
  • 3
  • 11
  • 31
August
  • 12,410
  • 3
  • 35
  • 51
  • Could you please paste complete code/class? And have you searched google "Syntax error on tokens, delete these tokens" before asking this question? – ritesh Oct 06 '13 at 03:21
  • @ritesh I just tried the first example on this page: http://docs.mongodb.org/ecosystem/tutorial/getting-started-with-java-driver/#getting-started-with-java-driver – August Oct 06 '13 at 03:33

1 Answers1

0

Try to put the statements in class as follows:

    import com.mongodb.MongoClient;
    import com.mongodb.MongoException;
    import com.mongodb.WriteConcern;
    import com.mongodb.DB;
    import com.mongodb.DBCollection;
    import com.mongodb.BasicDBObject;
    import com.mongodb.DBObject;
    import com.mongodb.DBCursor;
    import com.mongodb.ServerAddress;

    import java.util.Arrays;

    public Class MongoSample{

    // To directly connect to a single MongoDB server (note that this will not auto-discover the primary even
    // if it's a member of a replica set:
    MongoClient mongoClient = new MongoClient();
    // or
    MongoClient mongoClient = new MongoClient( "localhost" );
    // or
    MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
    // or, to connect to a replica set, with auto-discovery of the primary, supply a seed list of members
    MongoClient mongoClient = new MongoClient(Arrays.asList(new ServerAddress("localhost", 27017),
                                          new ServerAddress("localhost", 27018),
                                          new ServerAddress("localhost", 27019)));

    DB db = mongoClient.getDB( "mydb" );
}

And please try to investigate the issue on google/stackoverflow before posting it. You will learn a lot and would be able to help others.

I have taken the cue of possible problem from this answer

Community
  • 1
  • 1
ritesh
  • 907
  • 3
  • 11
  • 31
  • Ok, that gets rid of all those token errors, but now there is an error for `MongoClient mongoClient = new MongoClient( "localhost" );`, with no suggestions. – August Oct 06 '13 at 04:19
  • you have to try one of the MongoClient from all possible clients...If you are getting new errors then please try to read documentation, search on google and if still not able to find any credible answer then post as separate question on stackoverflow...If I have answered your current question then please accept the answer. – ritesh Oct 06 '13 at 04:23
  • @August: If you want to learn MongoDb, then you can attend their clases on Class-central->10gen.com (http://www.class-central.com/mooc/599/10gen-m101j-mongodb-for-java-developers) – ritesh Oct 06 '13 at 04:25