Michael
Michael

Reputation: 591

How to run a MongoDB query in Java

I have queries copied from the MongoDB tutorial, but they are not recognized in IntelliJ. Is it because the driver is version 1.8.2? I can't figure out how to update this to a newer version.

I've taken the example query shown below from https://mongodb.github.io/mongo-java-driver/4.1/driver/getting-started/quick-start/

However, intellij tells me that it cannot resolve the method eq.

myDoc = collection.find(eq("i", 71)).first();
System.out.println(myDoc.toJson());

Or this query. IntilliJ will tell me that the colon is an unexpected token

db.inventory.find( { status: { $in: [ "A", "D" ] } } )

enter image description here

Upvotes: 0

Views: 519

Answers (2)

Gibbs
Gibbs

Reputation: 22974

I suspect you are missing the static import.

import static com.mongodb.client.model.Filters.eq;

Try adding this. It will not throw compilation error. Please check the import statement against the version of java driver.

Upvotes: 1

bjorke07
bjorke07

Reputation: 119

However, intellij tells me that it cannot resolve the method eq.

Did you added the MongoDB dependency to Maven?

<dependencies>
<dependency>
    <groupId>org.mongodb</groupId>
    <artifactId>mongodb-driver-sync</artifactId>
    <version>4.1.1</version>
</dependency>

Upvotes: 0

Related Questions