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 value can't be queried if there's an index on the field #201

Closed
askoog opened this issue Sep 2, 2022 · 2 comments
Closed

array value can't be queried if there's an index on the field #201

askoog opened this issue Sep 2, 2022 · 2 comments

Comments

@askoog
Copy link

askoog commented Sep 2, 2022

Given a database index on a field where the values are an array, it is not possible to find the document using an exact document template, you'll need to specify an $all option.

Example java code:

InMemoryMongoServer server = new InMemoryMongoServer();
server.bind();
MongoClient mongoClient = new MongoClient(new ServerAddress(server.getLocalAddress()));
MongoDatabase db = mongoClient.getDatabase("db");
MongoCollection<Document> collection = db.getCollection("coll");


collection.createIndex(new Document("key", 1), new IndexOptions().unique(true));
collection.insertOne(new Document("key", Arrays.asList("val1", "val2")));

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

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

According to the mongo documentation, I'd expect the first query to also return the document, which is also the case if I skip the index creation.

A long shot guess is that the code handling index updates maybe also should create an entry for the array data as well?

Thanks for a great tool by the way!

@bwaldvogel
Copy link
Owner

Thanks for reporting this issue. It should be fixed with 36a4bb3. You could re-test with the latest version on main?

@bwaldvogel
Copy link
Owner

The fix is available in version 1.42.0.

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

2 participants