spring-projects / spring-projects/spring-data-mongodb

$in on array fields

Open
#3,528 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

type: bug
Dominant language
Java
Stars
1.7k
Forks
1.1k
PR merge metrics
No merged PRs in 30d

Description

Using spring-data-mongodb 3.1.2, my repo looks like:

@Repository
public interface AirBnbRepository extends  MongoRepository<Listing, String>,
										   QuerydslPredicateExecutor<Listing> {
}

The doc has a field thats a string array:

"amenities": ["TV", "Cable TV",

I'm trying to build a dynamic query with dsl, so something like:

BooleanBuilder builder = new BooleanBuilder();

if ((amenities != null) && (amenities.length > 0))
builder.and(QListing.listing.amenities.in(amenities));


This resulted in: find using query: { "amenities" : ["Wifi2"]}, which is wrong since its missing the whole $in portion. I stepped through the code and found there is a code path in there that if you have only 1 element, you change it to an eq. So I tried 2. I also tried bypassing the whole thing with:

builder.and(Expressions.booleanOperation(Ops.IN, QListing.listing.amenities, ConstantImpl.create(amenities)));

and got the same result. No $in.

How can we get the $in to work on an array with a dynamic query at runtime? I do also need to have pageable support :).

Thanks.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the QuerydslPredicateExecutor path used by AirBnbRepository and the MongoDB query rendering for QListing.listing.amenities.in(amenities), reproducing the reported one- and two-element cases with pageable support. Done means a dynamic query against the string array produces the intended MongoDB $in portion rather than an array equality query.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, mongodb
Domain
database
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.