简体   繁体   中英

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" ] } } )

在此处输入图片说明

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.

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>

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