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.
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.
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"} } ])
Java Mongo Code:
Required libraries :
mongodb-driver-core-3.12.4.jar
mongo-java-driver-3.12.4
Output
Make data easy with Helical Insight.
Helical Insight is world’s best open source business intelligence tool.
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
Best Open Source Business Intelligence Software Helical Insight Here
A Business Intelligence Framework
Best Open Source Business Intelligence Software Helical Insight is Here