gpt4 book ai didi

mongo-java-driver - 类型不匹配 : cannot convert from MongoCollection to DBCollection

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

我正在尝试将 spring-data-mongodb 从 1.5 升级到 2.1.0.M3所以我修改了pom依赖:

    <dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.5.0.RELEASE</version>
</dependency>

到2.1.0.M3

这是一个在 1.5 上运行良好的示例方法

@Override
public List<DBObject> getNews() {
DBCollection collection = mongoTemplate.getCollection(DbCollections.news);
DBObject query= getIsDeletedCondition(new BasicDBObject(),
DbColsNews.isDeleted,Boolean.FALSE);
DBCursor myCursor = collection.find(query).sort(new BasicDBObject(DbColsNews.dateAdded,1));
return myCursor.toArray();
}

我收到的错误是:

Type mismatch: cannot convert from MongoCollection<Document> to DBCollection

当我修改该行时:

 DBCollection collection = mongoTemplate.getCollection(DbCollections.news);

 MongoCollection<Document> collection = mongoTemplate.getCollection(DbCollections.news);

我收到错误

  The method find(Class<TResult>) in the type MongoCollection<Document> is not applicable for the arguments (DBObject)

在这一行:

DBCursor myCursor = collection.find(query).sort(new BasicDBObject(DbColsNews.dateAdded,1));

mongo-java-driver/spring-data-mongodb 的正确升级过程是什么?

最佳答案

尝试创建一个过滤器以传递给 find() 方法。

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

MongoCollection<Document> collection = new MongoClient().getDatabase("Database").getCollection("collection");
var myDocument = collection.find(eq("id", "Abc")).first();
System.out.println(myDocument.toJson());

For More Take Reference from here

关于mongo-java-driver - 类型不匹配 : cannot convert from MongoCollection<Document> to DBCollection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51201173/

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