今天參考網上MongoDB的例子(https://www.cnblogs.com/wenbronk/p/6385935.html)寫了個demo,發現MongoTemplate的getCollection方法返回值和例子上的不一樣,就在想是不是版本的問題,然後就去翻了官方的API文檔,發現這個方法,在1.9.x的時候還是com.mongodb.DBCollection類型,2.0以後就改成了com.mongodb.client.MongoCollection的類型,下面貼上兩個版本的API參考下
MongoTemplate
版本:1.9.x及之前(https://docs.spring.io/spring-data/data-mongo/docs/1.9.x/api/)
public com.mongodb.DBCollection getCollection(String collectionName)
Description copied from interface:
MongoOperations
Get a collection by name, creating it if it doesn't exist.
Translate any exceptions as necessary.
- Specified by:
-
getCollection
in interfaceMongoOperations
- Parameters:
-
collectionName
- name of the collection - Returns:
an existing collection or a newly created one.
版本:2.0.0及之後(https://docs.spring.io/spring-data/data-mongo/docs/2.0.0.BUILD-SNAPSHOT/api/)
public com.mongodb.client.MongoCollection<org.bson.Document> getCollection(String collectionName)
Description copied from interface:
MongoOperations
Get a collection by name, creating it if it doesn't exist.
Translate any exceptions as necessary.
- Specified by:
getCollection
in interfaceMongoOperations
- Parameters:
collectionName
- name of the collection. Must not be null.- Returns:
- an existing collection or a newly created one.
是不是DBCollection這個類有什麼bug,後來在版本升級的時候改變了方法的返回類型?