How to get Jongos MongoCollection with JongoNative
markhughes opened this issue · 6 comments
Using 1.4-early-20160606-0118.
Perhaps I've misunderstood but to use the latest MongoDB java driver we are supposed to use the new JongoNative.
But, it only returns the MongoCollection from MongoDB (com.mongodb.client.MongoCollection
) unlike before (org.jongo.MongoCollection
).
How do we go about getting org.jongo.MongoCollection
from JongoNative? Or, are we required to use the MongoDB BSON filters now?
This because org.jongo.MongoCollection
can only be used with deprecated DB instance from mongo java driver. JongoNative
returns com.mongodb.client.MongoCollection
.
You can still use Jongo as follow :
JongoNative jongo = Jongo.useNative(database, mapper);
com.mongodb.client.MongoCollection collection = jongo.getCollection("raw");
collection.findOne(jongo.query("{name : #}", "Abby"));
com.mongodb.client.MongoCollection collection = jongo.getCollection("pojo", Pojo.class);
collection.insertOne(pojo);
...
You can find more examples here : https://github.com/bguerout/jongo/tree/master/src/test/java/org/jongo/use_native
Let me know what do you think about this new API.
Ahh okay, I was basing it off the examples on the website. Oops, this works fine :)
I'm working on nodejs stack at the moment and I no longer use mongo-java-driver + jongo so i'm really interested in your feedback about how it is easy to use JongoNative with com.mongodb.client.MongoCollection
(jongo.query....)
I'm about to have a go at putting it into a project, so i'll let you know how I go!
Looks like it is just "find" not "findOne" now,
Like:
User user = collection.find(query).first();
I simplified it a bit by creating a small helper method in my base class (everything here is very abstract haha) - makes it a bit tidier:
public Optional<T> get(String id) {
T result = collection.find(q("{id : #}", id)).first();
if (result == null) return Optional.empty();
return Optional.of(result);
}
public Bson q(String query, Object... parameters) {
return this.jongo().query(query, parameters);
}
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.