Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error with mongoDB java driver

Tags:

java

mongodb

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

like image 811
user2014377 Avatar asked Jun 12 '15 10:06

user2014377


People also ask

Is there a JDBC driver for MongoDB?

The MongoDB Atlas SQL JDBC Driver provides SQL connectivity to MongoDB Atlas for client applications developed in Java. See the Atlas SQL Documentation for more information. MongoDB Atlas SQL JDBC is a JDBC Type 4 driver compatible with the JDBC 4.2 specification.

Is MongoDB compatible with Java?

All features are supported.

What is the database driver for MongoDB?

The JDBC driver for MongoDB allows SQL queries on MongoDB for any software that supports JDBC. SQL support includes functions, expressions, aggregation, and joins including for collections with nested objects and arrays. See the features and SQL examples supported by the JDBC driver for MongoDB.


2 Answers

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.

like image 64
NDB Avatar answered Oct 10 '22 22:10

NDB


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

like image 27
OldHu Avatar answered Oct 10 '22 23:10

OldHu