MongoDB document to POJO - mongodb

I'm using allanbank async driver for operations on mongodb. Is there any api available through which I can convert returned Document to a POJO, like we can do in spring driver available for mongodb

You could use morphia (which would mean using mongodb's java driver, or you could use jackson to map back to your POJOs.

You can use Spring Data , it includes integrated object mapping between documents and POJOs.

I wanted something for async driver as well. As MongoDB came with Codec Feature in 3.0 version (both sync and async version of driver), it's not neccessary to use libraries such as Morphia anymore so I wrote simple library for mapping POJO into MongoDB myself.

in Spring Data MongoDB you can use
#Autowired
MongoConverter mongoConverter;
Then
TARGET_OBJECT = mongoConverter.read(YOUR_TARGET.class, YOUR_DOCUMENT);

Related

Can ReactiveMongo can be used in Lagom framework?

I need to convert a existing Play + Scala + Mongodb application into Lagom micro services. But all the examples I have tested were using Cassadra.
There is Read Support for mongodb, as mentioned in their documentation.
There is plugin too.
This means, can we not write into mongodb collections?
Yes, you notionally can create a MongoDB persistence API. You'll need to implement:
com.lightbend.lagom.scaladsl.persistence.{
PersistenceComponents,
PersistentEntityRegistry,
ReadSidePersistenceComponents,
WriteSidePersistenceComponents
}
and play.api.db.DBComponents.
You can see an example implementation here for JDBC.

Specify raw aggregate request with Spring Data Mongodb

I write a big aggregate query for mongodb, now I try to translate it in Spring Data Mongodb API, and it's very complicated, and spring data api did not help me a lot.
So like with #Query annotation, is it possible to just specify my raw aggregate query in text and map my field with Spring Data (or just Mongodb Java driver) ?
I won't c/p my aggregate request because, it's not the purpose of my question.
I found a solution by using MongoDB java driver, which is available through Spring Data :
DBCollection collection = mongoTemplate.getCollection("myCollection");
and I used BasicDBObject from this solution : MongoDB aggregation with Java driver

How to use DBCursor provided in mongodb java driver in spring-data-mongodb?

I want to read around 1 million documents from my mongodb database and I am using spring data mongodb. I do not want to read all of 1 million data at once for performance reasons. Is there any way in spring-data-mongodb to do this. In raw java driver we have DBCursor.
One way i know is using pagination through repositories. Is there any other way in latest versions of spring data mongodb?
Yes. You can use pagination with spring data mongodb. MongoRepository extends from PagingAndSortingRepository which means you can call findAll(Pagable) and provide page information.
Alternatively, you can use mongoOperations/mongoTemplate to get a DBCollection reference, and then call find() on the collection and that will return you the DBCursor you want.

Using Java can one store BSON documents in MongoDB directly bypassing the DBObject API?

I think Jongo does it by using the Jackson pipeline to efficiently convert a POJO into the respective BSON, thus bypassing any intermediaries, like DBObject. However, it depends on Jackson 2.x (now FasterXML) and I am not in a position to upgrade from Jackson 1.9.x, so it seems I cannot use Jongo.
So, I am using the standard Java mongo driver, which requires me to map a POJO into the respective DBObject (see Efficient POJO mapping to/from Java Mongo DBObject using Jackson), which I suppose is marshalled to a BSON buffer by the driver at some point before being sent to the Mongo server process.
My question is this - can I prepare the BSON buffer myself and just hand it over to the driver?

mongodb persistence patterns for JSON client app, jackson mapper or morphia driver?

I've started a new job where they are using mongodb in a java environment.
They have implemented a pattern using DTOs and factories with the morphia driver, this may be due to a migration onto mongodb from a key value store previously. The client is a JSON client.
It seems to me that the jackson-mongo-mapper would be a better approach because it's just mapping pojos from json to BSON and back, seems like it could do away with all DTO factory facade?
Anyone know any pros and cons with these different approaches?
Spring Data for Mongodb is very nice since you can use even another data store or mix them and repository interface is very helpful.
Kundera is an option through JPA2
http://agilemobiledeveloper.wordpress.com/2013/08/22/working-with-mongodb-using-kundera/
There's a lot of java to mongodb options.
http://www.agilemobiledeveloper.com/2013/01/31/hibernate-ogm-mongodb-vs-kundera-vs-jongo-vs-mongodb-api-vs-morphia-vs-spring-data-mongo-mongodb-drivers-for-java/
Adding your own data layer and making sure you use DI and test it fully is very helpful.
NOSQLUnit is awesome -> https://github.com/lordofthejars/nosql-unit
DTOs are good for keeping a separation between implementation and design, so when they need or want to switch from mongo to some other NoSQL or SQL database it can be done cleanly.