spring-projects / spring-projects/spring-data-mongodb
$in on array fields
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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