我在mongodb tutorial for java中发现了关于如何从mongo collection中进行查询,但eq
他们使用的不适合我!你知道如何使用mongo和java过滤文档吗?为什么eq不存在mongo-java-driver?
这是我的尝试:
package Database;
import org.bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
public class StackOverflow {
public static void main(String[] args) {
// insert something to mongo:
final String URI = "mongodb://localhost:27017";
final String DB = "StackOverflowQuestion";
final String COLLECTION = "eqDoesntExcist";
MongoClientURI connection = new MongoClientURI(URI);
MongoClient mongo = new MongoClient(connection);
MongoDatabase database = mongo.getDatabase(DB);
MongoCollection<Document> collection = database.getCollection(COLLECTION);
Document doc = new Document("name", "Troy").append("height", 185);
collection.insertOne(doc);
doc = new Document("name", "Ann").append("height", 175);
collection.insertOne(doc);
// read something from mongo
FindIterable<Document> findIt = collection.find(eq("name", "Troy"));
// ERROR!!! the method eq(String, String) is undefined!
mongo.close();
}
}
我想是这样的:
SELECT * from eqDoesntExcist WHERE name = "Troy"
那么为什么他们的教程是错误的? –
@WW教程没有错,已经更新了答案。要使用这种格式,您需要包含正在使用的方法的静态导入。在你的情况'Filters.eq'。 – nullpointer