Convert MongoDB Native Queries to Java Mongo Queries

Posted on by By admin, in Databases | 0

Introduction:
Organizations of all sizes are adopting MongoDB because it enables them to build applications faster, handle highly diverse data types, and manage applications more efficiently at scale.

Make data easy with Helical Insight.
Helical Insight is world’s best open source business intelligence tool.

Lets Start Free Trail

Developing real-time BI dashboards and reports on top of nested and complex Mongo data is challenging as we cannot run the Mongo native queries directly from the Java applications.

So, for java applications to interact with Mongo DB and retrieve data we need to code the functionality of the Mongo native queries in Java using available drivers.

In this blog we are going to learn on how to convert Mongo native queries to Java Mongo queries.

Data-set:

We will be using the attached sample data-set for this exercise.

Sample_Dataset.json

The requirement is to find out the count by country.

Mongo Native Query:

db.sample_test.aggregate([
  {
    $group : {
       _id :  "$country",
       count: { $sum: 1 }
    }
  },
  {
     $project : { _id :0, country : "$_id", count : "$count"}
         }
 ])

MongoDB Query

Java Mongo Code:

Required libraries :

mongodb-driver-core-3.12.4.jar

mongo-java-driver-3.12.4

MongoDB Query

Output

Make data easy with Helical Insight.
Helical Insight is world’s best open source business intelligence tool.

Lets Register For Live Demo

MongoDB Query

Conclusion

Similarly, we can convert complex mongo native queries into Java mongo queries and execute them. This way, the java applications can fetch data from the mongo driver. To further convert the resultant java mongo output into a result-set, we would need to develop a driver that does the required job for us(not covered in this blog).

Thank You
Sai Charan
Helical IT Solutions Pvt Ltd

logo

Best Open Source Business Intelligence Software Helical Insight Here

logo

A Business Intelligence Framework


logo

Best Open Source Business Intelligence Software Helical Insight is Here

logo

A Business Intelligence Framework

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments