M
M
mrsakura522021-12-06 20:34:12
MongoDB
mrsakura52, 2021-12-06 20:34:12

Error connecting to MongoDB. How to fix?

I am trying to connect to mongodb:

MongoClient mongoClient = MongoClients.create("mongodb link");
MongoDatabase database = mongoClient.getDatabase("testdb");
MongoCollection<Document> paidsCollection = database.getCollection("test");

Error occurs java.lang.NoClassDefFoundError: com/mongodb/client/MongoClients
Dependency in pop.xml is set:
<dependency>
    <groupId>org.mongodb</groupId>
    <artifactId>mongo-java-driver</artifactId>
    <version>3.12.7</version>
</dependency>

I'm using the IntelliJ IDEA Community Edition 2021.2.3 development environment.
I read about it, but I don't understand how to see if I have all installed or not.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
nowm, 2021-12-07
@mrsakura52

There is no MongoClients class (with an s at the end) in the MongoDB Driver for Java. There is a MongoClient class.
So, you need to write like this:

MongoClient mongoClient = MongoClient.create("mongodb link");

And if you do it somewhere import com.mongodb.client.MongoClients, delete it, it's not right.
Edit: In some driver versions, the MongoClient class does not have a static create method. In this case, the client is created like this:
MongoClient mongoClient = new MongoClient("mongodb link");

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question