gpt4 book ai didi

java - 如何在 Eclipse 中使用 java Mongo 驱动程序 3.12.0 使用 MongoDB 中的多个字段查询文档

转载 作者:行者123 更新时间:2023-12-01 17:23:01 26 4
gpt4 key购买 nike

使用FindIterable<Document> findIT1 = custDetails.find(and(eq("customer.Fname", "PQR"), eq("customer.Lname", "Mnl")));

我得到了错误消息为

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
The method and(Bson, Bson) is undefined for the type JavaMongoConnection

无论我在互联网上搜索什么,我都会得到使用 and 运算符的相同答案,但是为什么它说它未声明?请问有人可以帮忙吗?

最佳答案

连接后,您必须指定要使用的集合。该错误消息表明 custDetails 是一个 JavaMongoConnection,而不是一个集合。

快速入门位于 https://mongodb.github.io/mongo-java-driver/3.4/driver/getting-started/quick-start/#access-a-collection

建议这样做来获取集合:

MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
MongoDatabase database = mongoClient.getDatabase("mydb");
MongoCollection<Document> collection = database.getCollection("test");

然后您应该能够对该集合使用findand

关于java - 如何在 Eclipse 中使用 java Mongo 驱动程序 3.12.0 使用 MongoDB 中的多个字段查询文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61256024/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com