Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Array values cannot be queried along with multiple other fields if there is a compound index #209

Open
MelvinFrohike opened this issue Jan 10, 2023 · 0 comments

Comments

@MelvinFrohike
Copy link

This issue is very similar to the closed issue here #201

The difference here is that querying for exact array matches does not work when querying for three fields where one is an array and there is a compound index on all of them.
The query works fine if there is no compound index or if only two of the fields are queried.

Example java code:

MongoCollection<Document> collection = mongoTemplate.getCollection("coll");
        Index index = new Index()
            .on("key", Sort.Direction.ASC)
            .on("foo", Sort.Direction.ASC)
            .on("bar", Sort.Direction.ASC)
            .unique();

        mongoTemplate.indexOps("coll").ensureIndex(index);

        collection.insertOne(new Document(Map.of(
            "key", Arrays.asList("val1", "val2"),
            "foo", "someFoo",
            "bar", "someBar"
        )));

// search for { "key" : [ "val1", "val2" ], "foo": "someFoo", "bar": "someBar" }
        FindIterable<Document> documents = collection.find(new Document(
            Map.of(
                "key", Arrays.asList("val1", "val2"),
                "foo", "someFoo",
                "bar", "someBar"
            )
        ));
        List<Document> collect = StreamSupport.stream(documents.spliterator(), false).collect(Collectors.toList());
// no entries found
        System.out.println(collect);

// search for { "key" : [ "val1", "val2" ], "foo": "someFoo"}
        documents = collection.find(new Document(
            Map.of(
                "key", Arrays.asList("val1", "val2"),
                "foo", "someFoo"
            )
        ));
        collect = StreamSupport.stream(documents.spliterator(), false).collect(Collectors.toList());
// finds the documents
        System.out.println(collect);

// search for { "key" : { "$all" : [ "val1", "val2" ]}, , "foo": "someFoo", "bar": "someBar"}
        documents = collection.find(new Document(
            Map.of(
                "key", new Document("$all", Arrays.asList("val1", "val2")),
                "foo", "someFoo",
                "bar", "someBar"
            )));
        collect = StreamSupport.stream(documents.spliterator(), false).collect(Collectors.toList());
// finds the document
        System.out.println(collect);

I would expect the first query to return results as well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant