Deperecated `getDB()`
FutureElement opened this issue · 10 comments
new MongoClient().getDB("dbname"); getDB isDeperecated
@Stephan202 i dont think it‘d fixed
@FutureElement, that's correct; #254 is still open at this time.
@Stephan202 how do u solve this
@FutureElement we declare a Spring bean as follows (code slightly simplified, but you get the idea):
...
import com.mongodb.MongoClient;
import org.jongo.Mapper;
...
@Bean
Jongo jongo(MongoClient client, Mapper mapper, @Value("${gc.mongo.dbname:}") String dbName) {
@SuppressWarnings("deprecation")
DB db = client.getDB(dbName);
return new Jongo(db, mapper);
}
We compile with -Xlint:all
and -Werror
, so the suppression prevents a build failure.
Hi there,
1.3 relies on the deprecated mongo-java-driver v2 api.
Note that 1.4 we still propose the deprecated API (new Jongo(db, mapper)
/ backward compatibility).
1.4 will propose a new api to deal with the new mongo-java-driver v3 api.
Here is an example:
@Test
public void jongoV3Example() throws Exception {
//Get database instance from new driver API
MongoDatabase database = mongoClient.getDatabase("test_jongo");
//Add Jongo capabililities to new MongoCollection (query, jackson)
JongoNative jongo = Jongo.useNative(database);
com.mongodb.client.MongoCollection<Friend> friends = jongo.getCollection("friends", Friend.class);
Friend friend = new Friend("Robert");
// Use the Jackson configuration provided by Jongo with new driver methods.
// No need to change your POJOs
friends.insertOne(friend);
//Write query just like before
Friend robert = collection.find(jongo.query("{name:'#'}", "Robert")).first();
...
}
You can find more examples here : https://github.com/bguerout/jongo/tree/master/src/test/java/org/jongo/use_native
I'm not presently working on either java or mongo, so it you be great to have feeback about this new API before releasing it.
<dependencies>
<dependency>
<groupId>org.jongo</groupId>
<artifactId>jongo</artifactId>
<version>jongo-1.4-early-20171013-1609</version>
</dependency>
</dependencies>
...
<repositories>
<repository>
<id>cloudbees-jongo-early-release</id>
<url>http://repository-jongo.forge.cloudbees.com/release</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
@bguerout Can you please tell which github version of jongo is stable? I want to use com.mongodb.MongoDatabase as getDB() is deprecated. Master version of jongo github is still using com.mongodb.DB. Please help.
@bguerout Any reason that you reverted your approach of JongoNative ? Also, will the plan be a major release of Jongo 2.x something which will not be backward compatible / deprecate the existing getDB api and introduce a new API like JongoNative?
You can query by using Mongo Template. Example below:
// eg: queryString = "{ 'status': 'A' }"
BasicQuery query = new BasicQuery(queryString);
List<ExampleModel> result = mongoTemplate.find(query, Example.class);
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.