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

[Collection/Tables] - Allows dot notation on objects returned by the API #38

Open
clun opened this issue Jan 27, 2025 · 0 comments
Open
Labels

Comments

@clun
Copy link
Collaborator

clun commented Jan 27, 2025

1. Building document with appendor put

We want to use a dot notation to populate a document and create the needed submaps as we go

Document doc = new Document();
doc.append("field1", "hello");
doc.append("metadata.key1", "value1");
doc.append("metadata.key2.key11", "value11");
System.out.println(doc.toJson());

Gets you a document without sub map definition

{"field1":"hello","metadata":{"key1":"value1","key2":{"key11":"value11"}}}

Sub map definitions are still possible:

Document doc = new Document();
doc.append("field1", "hello");
Map<String, String> map = new HashMap();
map.put("key11", "value11");
Map<String, Object> metadata = new HashMap();
map2.put("key1", "value1");
map2.put("key2", map);
doc.append("metadata", metadata);

2. Check existence with containKey

Dot notation is also working with containKey

Assertions.assertTrue(doc.containsKey("metadata.key2"));
Assertions.assertTrue(doc.containsKey("metadata.key2.key11"));
Assertions.assertFalse(doc.containsKey("metadata.key2.key12"));

3. remove

Dot notation is also working with remove

doc.remove("metadata.key2.key11");
Assertions.assertFalse(doc.containsKey("metadata.key2.key11"));
System.out.println(doc.toJson());

Gets you

{"field1":"hello","metadata":{"key1":"value1","key2":{}}}

4. Accessing Document/Rows fields (GET) also use by distinct

API currently returning Documents and Rows but we would like with the Java Client to be table to retrieve nested data:

// Nested Maps
myDocument.get("metadata.key1");

// Nested Arrays
myDocument.get("metadata.keyArray[0]");

Implementations notes:

  • No error if the field does not exist, simply return null
  • No error if using negative indice or indice bigger than the list size

5. Sample applied to distinct

Collection<Document> ccc = getCollection(true);
        List<Document> animal = new ArrayList<>();
        animal.add(new Document("1")
                .append("name", "Kittie")
                .append("metadata", Map.of("animal", "cat", "color", "black")));
        animal.add(new Document("2")
                .append("name", "Lassie")
                .append("metadata", Map.of("animal", "dog", "breed", "Rough Collie", "color", "brown and white")));
        animal.add(new Document("3")
                        .append("name", "Dolly")
                        .append("metadata", Map.of("animal", "sheep", "breed", "Finn-Dorset", "cloned", "yes")));
        animal.add(new Document("4")
                        .append("name", "Marjan")
                        .append("metadata", Map.of("animal", "lion", "location", "Kabul Zoo", "fame", "war survivor")));
        animal.add(new Document("5")
                        .append("name", "Clever Hans")
                        .append("metadata", Map.of("animal", "horse", "fame", "math abilities")));
        animal.add(new Document("6")
                        .append("name", "Paul")
                        .append("metadata", Map.of("animal", "octopus", "fame", "World Cup predictions")));
        animal.add(new Document("7")
                        .append("name", "Hachiko")
                        .append("metadata", Map.of("animal", "dog", "breed", "Akita", "fame", "loyalty")));
        animal.add(new Document("8")
                        .append("name", "Balto")
                        .append("metadata", Map.of("animal", "dog", "breed", "Siberian Husky", "fame", "serum run hero")));
        animal.add(new Document("9")
                        .append("name", "Babe")
                        .append("metadata", Map.of("animal", "pig", "fame", "movie star")));
        animal.add(new Document("10")
                        .append("name", "Togo")
                        .append("metadata", Map.of("animal", "dog", "breed", "Siberian Husky", "fame", "real serum run hero")));
        ccc.insertMany(animal);

        List<String> races = ccc.distinct("metadata.animal", String.class).all();
        System.out.println(races);

gets you

[dog, cat, pig, horse, octopus, lion, sheep]
@clun clun added the fixed label Jan 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant