3

I am trying to upgrade spring-data-mongodb from 1.5 to 2.1.0.M3 so I modified the pom dependency from :

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

to 2.1.0.M3

here is a sample method that used to work fine with 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();
}

The error I am getting is :

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

When I modify the line:

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

to

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

I get the error

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

on this line:

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

what is the correct upgrade procedure for the mongo-java-driver/spring-data-mongodb ?

JavaSheriff
  • 7,074
  • 20
  • 89
  • 159

3 Answers3

1

Try to create a filter to pass to the find() method.

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

Hitesh Anshani
  • 1,499
  • 9
  • 19
1

I had this problem when I am upgrading my Spring Boot version to 2.X which also updates my Mongo Driver version. So when we are changing from DBCollection to MongoCOllection there are lot of differences in APIs. For example instead of using findOne(query) I had to use find(query).first(). The find() method in MongoCollection returns FindIterable while DBCollection retuns a DBcursor. So, when I am upgrading my Mongo Java Driver version I had no option than to change my Mongo Stuff according to the new MongoCollection methods. Also I have used BasicDBObject before, now I have got rid of it and using Document instead because there is a casting error when I tried to convert BasicDBObject to a Document. I haven't found any work around. So, probably you have to change your Mongo related stuff.

Jay
  • 132
  • 1
  • 4
0

I had similar issue when upgrading to spring-boot 2.0.4, and there is not much info available for solution around this.

However, simple casting to DBCollection worked for my app. Here is a snippet:

DBCollection dbCollection = (DBCollection) mongoTemplate.getCollection("collectionName");
Nitin1706
  • 621
  • 1
  • 11
  • 21