Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8450791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:04:16+00:00 2026-06-10T11:04:16+00:00

I am using spring-data mongo with the JSON based query methods, and am unsure

  • 0

I am using spring-data mongo with the JSON based query methods, and am unsure how to allow optional parameters in a search query.

For instance – say I had the following function

@Query("{ 'name' : {$regex : ?0, $options : 'i'}, 'createdDate' : {$gte : ?1, $lt : ?2 }} }")
List<MyItem> getItemsLikeNameByDateRange(String name, Date startDateRange, Date endDateRange);

-but I didnt want to apply the name regex match, or not apply a date range restriction if NULL values were passed to the method.

At the moment it looks like I might have to build the query using the mongoTemplate.

Are there any alternatives – or is using mongoTemplate the best option?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T11:04:18+00:00Added an answer on June 10, 2026 at 11:04 am

    To implement this in Boolean logic I do the following and the conversion to operations that are available in programming languages

    :query != null -> field == :query
    !(:query != null) || (field == :query)
    (:query == null) || (field == :query)
    

    In plain SQL, this is done as

    where (null = :query) or (field = :query)
    

    In MongoDB this is done through the $where

    { $where: '?0 == null || this.field == ?0' } 
    

    We can speed this up a little by using Mongo Operations rather than building everything to the function at the expense of some readability. does not work unfortunately.

    { $or : [ { $where: '?0 == null' } , { field : ?0 } ] } 
    

    So what you have is

    @Query("{ $or : [ { $where: '?0 == null' } , { field : ?0 } ] }")
    List<Something> findAll(String query, Pageable pageable);
    

    This can be further expanded to handle arrays for in/all clauses

    @Query("{ $or : [ { $where: '?0.length == 0' } , { field : { $in : ?0 } } ] }")
    List<Something> findAll(String query, Pageable pageable);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Spring Data, Neo4j and Jackson to serve a JSON API. I have
I am using Mongo in its simplest avatar possible (in conjunction with Spring Data).
I'm mapping my request's JSON POST data into an object using Spring's @RequestBody annotation
I'm using Spring data neo4j 2.1.0.BUILD-SNAPSHOT and Neo4j 1.6.1 server. I have a Friendship
Whenever I specify a resource using the Spring Data Hadoop namespace, by application throws
I'm using Spring JdbcTemplate interface for fetching data from a MS SqlServer DB. In
Im using this code: protected String getContactInfo() { Cursor cursor = getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, null,
Using javascript, how do I add some data to the query string? Basically I
Using: Spring 3.1.0.RELEASE, Spring Data MongoDB 1.0.0.RELEASE I have a document class defined like
I've switched from using Spring Data JPA for regular DAO objects with Entity Manager

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.