1

I am getting this error on the last line in code below: The type org.bson.conversions.Bson cannot be resolved. It is indirectly referenced from required .class files, I am using Mongo JavaDriver 3.0.2

 import com.mongodb.BasicDBObject;
 import com.mongodb.DB;
 import com.mongodb.DBCollection;
 import com.mongodb.Mongo;
 import com.mongodb.MongoClient;
 import com.mongodb.util.JSON;

public class InsertDriver {

public static void main(String[] args) 
{

    Mongo mongo = new Mongo("localhost", 27017);
    DB db = mongo.getDB("postsdb");
    DBCollection collection = db.getCollection("posts");

    BasicDBObject doc1 = new BasicDBObject();
    doc1.put("user", "Mike");
    doc1.put("sports", "soccer");
    doc1.put("tweet", "Hi..");

    //String json = JSON.serialize( doc1);
    //BasicDBObject bson = (BasicDBObject) JSON.parse( json );

    List<BasicDBObject> docs = new ArrayList<BasicDBObject>();
    docs.add(doc1);

    collection.insert(docs);  //ERROR HERE

 }

}

1 Answer 1

3

Had the same issue. I've solved it by downloading the bson jar and adding it to the search path.

The Mongodb documentation states the following:

You can also download the jars directly from sonatype. Note: mongodb-driver requires the following dependencies: bson and mongodb-driver-core

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

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.