CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > How to > Search In MongoDB Using Java

Java Tutorial

  • Introduction
    • What is Java
    • History Of Java
    • Install Java
    • What is JVM
    • JDK vs JRE vs JVM
    • Java Bytecode
    • OOP vs POP
    • Compile and Run Java
  • Tokens, Expressions and Control Structures
    • Primitive data types
    • Integers
    • Floating Points
    • Characters
    • Booleans
    • User Defined Data Type
    • Declarations
    • Constants
    • Identifiers
    • Literals
    • Type Conversion and Casting
    • Variables
    • Default Variable Initialization
    • Command Line Arguments
    • Arrays of Primitive Types
    • Comment Syntax
    • Garbage Collection
    • Expressions
    • Operators
    • Arithmetic Operator
    • Bitwise and Shift Operator
    • Comparison or Relational Operators
    • Logical Operators
    • Assignment Operators
    • Ternary Operator
    • Increment and Decrement Operator
    • Control Statements
  • OOP Concepts
    • Class and Object
    • Create Class Instance
    • Method
    • Abstraction
    • Encapsulation
    • this keyword
    • Constructor
    • Pass by Value
    • Access Modifier/Control
    • Polymorphism
    • Method Overloading vs Method Overriding
    • Recursion
    • Nested and Inner Class
  • Inheritance and Packaging
    • Inheritance
    • extends Keyword
    • super Keyword
    • Object Class
    • Abstract class
    • Final Class
    • Java Package
    • Interface
  • Handling Error/ Exceptions
    • What is Exception
    • Exception Handling Keywords
    • Common Java Errors
    • User Defined Exception
    • Throwing and re-throwing Exception
    • finally Block
  • Strings
    • Java String Tutorial
  • Threads
    • Introduction
    • Create Thread
    • Thread Lifecycle
    • Thread Priority
    • Thread Synchronization
    • Inner Thread Communication
    • Thread Deadlock
  • IO and Streams
    • java.io Package
    • Files and Directories
    • Byte Stream
    • Character Stream
    • Console Input and Output
    • Serializable and Deserializable
  • Core Packages
    • java.lang Package
    • Math
    • Wrapper Classes
    • java.lang.Number
    • Double
    • Float
    • Integers
    • java.lang.Byte
    • java.lang.Short
    • java.lang.Long
    • java.lang.Character
    • java.lang.Boolean
    • java.util package
    • Vector Class
    • Stack Class
    • Dictionary Class
    • Hashtable
    • Enumeration or Enum
    • Generate Random Number
  • Holding Collection of Data
    • Arrays
    • Map
    • List
    • Set
    • Collection Interface
    • Collections Class
    • ArrayList
    • HashSet
    • TreeSet
    • Comparator
  • Java Bean
    • What is Java Bean
    • Advantages and Disadvantages of Java Bean
    • Java Beans API
    • Introspection
    • Java Bean Properties
    • Bound and Constrained Properties
    • BeanInfo Interface
    • Customizers
    • Java Beans Persistence
    • BeanDescriptor

Search In MongoDB Using Java

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Last updated: July 1, 2026 ยท 2 min read ยท 0 Comments
Share: in X
check MongoDb database size

In this post, we will learn to implement a search query in MongoDB using Java. Most of the applications have search functionality. So, learning to use it will be very useful.

1. Prerequisite

  • MongoDB installed
  • Java driver for MongoDB

2. SQL query example

select * from user where name like '%ai%';

This query fetches all the users whose name has a character ai together. It matches all the names given below:

Fraiser
Crug Air
Jaimin
Balam Jair
Isai
Again

3. MongoDB query in Java

3.1. Single word search

The MongoDB query equivalent to search a single word in Java is:

Filters.regex("name","ai", "i")

The complete example:

String search = "ai";
Bson searchQuery = Filters.regex("name",search, "i");
/*
*The following code returns the FindIterable object of user collection.
*/
userCollection.find(searchQuery);

In our example, we can see that the “i” options parameter to ignore case sensitivity. In most of the search features, we usually ignore the case sensitive so I’ve used it in here. If you want to match the words with the exact Case then you can simply remove that parameter and use a query like:

Bson searchQuery = Filters.regex("name",search);

3.2. Search multiple words

If we have multiple words in search like:

String search = "ai in"

Then, we can split the words by space and start creating separate queries for each word then combine them all together and use them as a single query.

Example:

String[] searchTokens = search.split(" "); // Spliting words by space

Create a list of Bson object:

List<Bson> searchQueries = new ArrayList<>();

Create a separate query for each word in the search:

for(String token: searchTokens){
    searchQueries.add(Filters.regex("name",token, "i"));
}

Now, use searchQueries to find matched content:

userCollection.find(Filters.or(searchQueries)); 

This query will return all the names that have either ai or in or both in their name.

Complete example code:

String search = "ai in";
String[] searchTokens = search.split(" "); // Spliting words by space
List<Bson> searchQueries = new ArrayList<>();
for(String token: searchTokens){
    searchQueries.add(Filters.regex("name",token, "i"));
}
// The Filters.or(searchQueries) will generats an OR operator so that 
// It matches any words given in search criteria
userCollection.find(Filters.or(searchQueries)); 

4. Summary

In this post, we learned to implement a search in MongoDB using Java.

Related Posts:

  • MySQL Commands for Developers
  • JDBC Driver Types Explained: Which One Should You Choose?
  • java.lang.Character Class in Java
  • JDBC Configuration
  • Character data type in Java
  • java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
Tags:javamongodb
Was this article helpful?
โ† Previous ArticleWhat is MongoDB?
Next Article โ†’Create Cluster In MongoDB Atlas

Leave a Comment Cancel reply

You must be logged in to post a comment.

Recent Posts

  • How to implement Passwordless Authentication in Spring Boot: A Step-by-Step Guide
  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Linkedin

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
ยฉ 2026 CoderSathi. All rights reserved. Privacy Policy ยท Sitemap