简体   繁体   中英

Error with mongoDB java driver

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

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.xxjar 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.

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

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>

您需要下载bson库(v3.0.2)并将其添加到类路径中: https//oss.sonatype.org/content/repositories/releases/org/mongodb/bson/3.0.2/bson-3.0。 2.jar

您应该检查此包是否在类路径中: http//api.mongodb.org/java/current/org/bson/package-summary.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM