#Java #MongoDB #Morphia #Map-Reduce

Map-Reduce with MongoDB and Morphia

Just a note to record the usage of Map-Reduce with MongoDB and Morphia. Firstly, add the Morphia dependency. <dependency> <groupId>org.mongodb</groupId> <artifactId>mongo-java-driver</artifactId> <version>2.11.2</version> </dependency> <dependency> <groupId>org.mongodb.morphia</groupId> <artifactId>morphia</artifactId> <version>0.105</version> </dependency> The syntax of MapReduceCommand in Morphia as shown in MapReduceCommand.java: public MapReduceCommand( DBCollection inputCollection, String map, String reduce, String outputCollection, OutputType type, DBObject query) { // Compiled Code } Here we want to group the sales amount (i.e., subtotal) of each county and exclude the null-subtotal from collection. ...

Author 2 min
#Java #MongoDB

Using the MongoDB Aggregation Framework via MongoDB Asynchronous Java Driver

Introduction I often use Morphia in projects for mapping Java objects to/from MongoDB and it’s Query API instead of building the complex DBObject query. However, the current version (v. 0.105) without aggregate command support. Fortunately, the MongoDB Asynchronous Java Driver provides the aggregate builder to construct complex pipelines of operators in fluent way. As usual, the following paragraphs will express some basic usages of mongodb-async-driver in aggregation pipeline framework through an example. ...

Author 3 min
#MongoDB #Morphia #Java #NoSQL

Using MongoDB with Morphia

Morphia - The JVM Object Document Mapper for MongoDB. MongoDB is an extremely useful document-based database in NoSQL field. There are various drivers and client libraries in MongoDB Ecosystem and detailed manual to facilitate the developers to get into it shortly. In the beginning, we use MongoDB Java driver to manipulate CRUD operations on databases. Everything is running well but lots of BasicDBObject cause the code lack of readability and fluency. ...

Author 3 min
#MongoDB

Converting ISODate from MongoDB

I’m confused about the insertion operation with Date object via mongo-java-driver that always short of 8 hours where my place of residence is Taiwan (GMT+8). According to the enclosed references, we can observe that the incoming Date object will be set to ISO_8601_DATE_FORMAT as shown in the following code snippet. if (o instanceof Date) { Date d = (Date) o; SimpleDateFormat format = new SimpleDateFormat(ISO_8601_DATE_FORMAT); serialize(new BasicDBObject("$date", format.format(d)), buf); return; } An example next demonstrates that the given Date will plus 8 hours in the GMT+8 time zone. ...

Author 1 min