Deperecated `getDB()` and soon to be deprecated DB classes
kilaka opened this issue · 9 comments
getDB is deprecated but instructed to be used by Jongo.
From mongo's class documentation:
The DB class has been superseded by MongoDatabase. The deprecation of this method effectively deprecates the DB, DBCollection, and DBCursor classes, among others; but in order to give users time to migrate to the new API without experiencing a huge number of compiler warnings, those classes have not yet been formally deprecated.
Jongo highly relies on the soon to be deprecated classes, like DB and DBCursor.
- Love Jongo :)
Hello,
You can find more informations here : #254
Here is working examples of Jongo with the new driver API : https://github.com/bguerout/jongo/tree/master/src/test/java/org/jongo/v3
What's missing for a 1.4 release that supports the new driver API?
The 1.4 is almost done, documentation needs to be updated.
Are you interested about testing an early release of this version ?
Cool! Sure, I'd be happy to test a RC version.
I downloaded the 1.4, but which object I've to use to avoid deprecated warnings?
import org.jongo.Jongo;
...
MongoClientURI connectionString = new MongoClientURI(ConfigFactory.load().getString("playjongo.uri"));
MongoClient mongoClient = new MongoClient(connectionString);
// Deprecated
DB database = mongoClient.getDB(ConfigFactory.load().getString("playjongo.database"));
// Should be use the following, but Jongo doesn't support MongoDatabase
//MongoDatabase database = MongoClient.getDatabase(ConfigFactory.load().getString("playjongo.database"));
Jongo jongo = new Jongo(database);
Here is a new beta version
<dependencies>
<dependency>
<groupId>org.jongo</groupId>
<artifactId>jongo</artifactId>
<version>jongo-1.4-early-20161223-1142</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>
Note that this version must be considered as a beta of the future 1.4.0 release
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 in test folder : https://github.com/bguerout/jongo/tree/master/src/test/java/org/jongo/v3
Let me know if you encounter any problem.
Now the query seems to be more complex or I wrong something. I'm trying to create a Play framework module to use with Jongo, so I created the module like the following:
public class PlayJongo {
public JongoNative jongo;
@Inject
public PlayJongo() {
this.jongo = initJongo();
}
public JongoNative initJongo() {
MongoClientURI connectionString = new MongoClientURI(ConfigFactory.load().getString("playjongo.uri"));
MongoClient mongoClient = new MongoClient(connectionString);
MongoDatabase database = mongoClient.getDatabase(ConfigFactory.load().getString("playjongo.database"));
JongoNative jongo = Jongo.useNative(database);
return jongo;
}
}
But I've trouble with queries, save and remove.
Before I could use:
public class UsersRepository {
@Inject
public PlayJongo jongo;
public MongoCollection users() {
return jongo.getCollection("Database.users");
}
public Users findByEmail(String email) {
return users().findOne("{email: #}", email).as(Users.class);
}
public MongoCursor<Users> findAll() {
return users().find("{role: {$nin: ['SUPERADMIN']}, removed: false}").sort("{lastname: 1}").as(Users.class);
}
Now I've to use:
public class UsersRepository {
@Inject
public PlayJongo jongo;
public MongoCollection<Users> users() {
return jongo.jongo.getCollection("DocBox.users", Users.class);
}
public Users findById(String id) {
return users().find(jongo.jongo.query("{_id: #}", new ObjectId(id))).first();
}
and I didn't find a way to have all records like findAll in the previous code. Also for save/modify and remove.
Before, from my controllers, I used:
public class User extends Controller {
@Inject
private UsersRepository users;
...
users.users().remove(u.getId());
...
// save new user or modify an existing one
users.users().save(u);
Now to save seems I've to use insertOne, but save worked also for updates, so to create a new record or update the existing one I used save. And now?
How to remove a record?
I think to wrong something, can you give me the right direction?
Thanks
This hasn't been updated in a while. What is the current status of the development of this and jongo?
Hello,
To be true the 1.4 version is almost done since lot of months (June 2016) and afaik only the documentation (ie. website) needs to be updated.
I'm not totally satisfied with v3 API and its test coverage (https://github.com/bguerout/jongo/tree/master/src/test/java/org/jongo/use_native).
It is very hard for me to find time to work on Jongo but i will do my best to finish what has been started.
Closed in favor of #254