Skip to content

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

@MelvinFrohike

Description

@MelvinFrohike

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions