DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Building a 3D WebXR Game with WASI Cycles: Integrating WasmEdge, Wasmtime, and Wasmer to Invoke MongoDB, Kafka, and Oracle
  • A Developer's Guide to Database Sharding With MongoDB
  • Kafka Link: Ingesting Data From MongoDB to Capella Columnar
  • Java and MongoDB Integration: A CRUD Tutorial [Video Tutorial]

Trending

  • Run Gemma 4 on Your Laptop: A Hands-On Guide to Google's Latest Open Multimodal LLM
  • Ujorm3: A New Lightweight ORM for JavaBeans and Records
  • The ORM Is Over: AI-Written SQL Is the New Data Access Layer
  • The Agent Protocol Stack: MCP vs. A2A vs. AG-UI
  1. DZone
  2. Data Engineering
  3. Databases
  4. Writing Complex MongoDB Queries Using QueryBuilder

Writing Complex MongoDB Queries Using QueryBuilder

By 
Rishav Rohit user avatar
Rishav Rohit
·
Nov. 25, 14 · Interview
Likes (2)
Comment
Save
Tweet
Share
51.1K Views

Join the DZone community and get the full member experience.

Join For Free

MongoDB provides a lot of query selectors for filtering documents from a collection.  Writing complex queries for MongoDB in Java can be tricky sometimes.
Consider below data present in student_marks collection

{"sid" : 1,"fname" : "Tom","lname" : "Ford","marks" : [ {"english" : 48}, {"maths" : 49}, {"science" : 50}]}
{"sid" : 2,"fname" : "Tim","lname" : "Walker","marks" : [ {"english" : 35}, {"maths" : 42}, {"science" : 37}]}
{"sid" : 3,"fname" : "John","lname" : "Ward","marks" : [ {"english" : 45}, {"maths" : 41}, {"science" : 37}]}

If we want to get students whose last name is Ford and have obtained more than 35 marks in english then the MongoDB shell command for this will be -

     db.student_marks.find({$and:[{"lname":"Ford"},{"marks.english": {$gt:35}}]})

The same query written in Java will look something like this -

     DBObject query = new BasicDBObject();
     List<BasicDBObject> andQuery = new ArrayList<BasicDBObject>();
     andQuery.add(new BasicDBObject("lname", "Ford"));
     andQuery.add(new BasicDBObject("marks.english", new BasicDBObject("$gt", 35)));
     query.put("$and", andQuery); 

Using MongoDB QueryBuilder we can rewrite above query as -


    DBObject query = new QueryBuilder()
         .start()
         .and(new QueryBuilder().start().put("lname").is("Ford").get(),
             new QueryBuilder().start().put("marks.english")
                 .greaterThan(35).get()).get();


You can see that by using QueryBuilder we can write complex queries with ease. QueryBuilder class provides many methods like and, not, greaterThan, exists, etc. which helps in writing MongoDB queries more efficiently and less prone to error/mistakes.

 



If you enjoyed this article and want to learn more about MongoDB, check out this collection of tutorials and articles on all things MongoDB.

Database MongoDB

Published at DZone with permission of Rishav Rohit. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Building a 3D WebXR Game with WASI Cycles: Integrating WasmEdge, Wasmtime, and Wasmer to Invoke MongoDB, Kafka, and Oracle
  • A Developer's Guide to Database Sharding With MongoDB
  • Kafka Link: Ingesting Data From MongoDB to Capella Columnar
  • Java and MongoDB Integration: A CRUD Tutorial [Video Tutorial]

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook