12

I am using mongodb java driver 3.0.2 (I tried 3.0.1 and 3.0.0 but I get the same error).

This is the error that I get: The type org.bson.Document cannot be resolved. It is indirectly referenced from required .class files

It happens on the second line of the following code:

MongoDatabase db = mongoClient.getDatabase("db");
db.getCollection("");

As I understand the second line uses code that exists in org.bson.Document and it cant find it. But where can I find it?

************** EDIT ************ I know added the bson jar and it has the org.bson package BUT there is not org.bson.Document class inside this package...

1
  • Are you sure you are using the Mongo Java Driver which contains several libraries, and no the MongoDB Driver for Java? see my answer below. Commented Nov 20, 2015 at 8:31

5 Answers 5

17

I believe you have downloaded the MongoDB-Driver for Java, and not the Mongo-Java-Driver.

Easiest way is to download the mongo-java-driver-3.x.x.jar library, available at http://mongodb.github.io/mongo-java-driver/

It contains the following packages:

  • MongoDB Driver
  • MongoDB Async Driver
  • BSON Library
  • Core Driver

If you install the MongoDB driver only , then you need to download BSON Library separately.

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

2 Comments

The mongoDB installation guide for the Java driver says "There are two Maven artifacts available in the 3.4 release. The preferred artifact for new applications is mongodb-driver however, we still publish the legacy mongo-java-driver uber-jar."
i am using mongo-java-driver 3.7.1, still i am unable to import the libraries.
5

Document class exist since from 3.0.0 and not in MongDB jar, it's in BSON jar file. You can download from https://oss.sonatype.org/content/repositories/releases/org/mongodb/bson/3.0.2/bson-3.0.2.jar

Reference:

public class Document extends Object implements Map, Serializable, Bson A representation of a document as a Map. All iterators will traverse the elements in insertion order, as with LinkedHashMap. Since: 3.0.0

http://api.mongodb.org/java/3.0/org/bson/Document.html

Comments

2

If you are using eclipse and maven change the dependency in pom.xml as follows:-

  <dependency>
    <groupId>org.mongodb</groupId>
    <artifactId>mongo-java-driver</artifactId>
    <version>3.4.2</version>
  </dependency>

Comments

1

You will need to download the bson library (v3.0.2) and add it to your classpath : https://oss.sonatype.org/content/repositories/releases/org/mongodb/bson/3.0.2/bson-3.0.2.jar

1 Comment

Please take care: in the meanwhile 3.0.2 is not the latest version, please check out the actual version at oss.sonatype.org/content/repositories/releases/org/mongodb/bson
0

You should check if this package is in the classpath: http://api.mongodb.org/java/current/org/bson/package-summary.html

2 Comments

Well, that's really strange... Are you sure that it is included in the classpath?
Yes it is it. The class Document is simply not there... I have no idea why

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.