[JAVA-1720] Add getDocument() and getArray() functions to the new Document class Created: 25/Mar/15  Updated: 02/Feb/19  Resolved: 31/Jan/19

Status: Closed
Project: Java Driver
Component/s: API
Affects Version/s: 3.0.0
Fix Version/s: 3.10.0

Type: New Feature Priority: Major - P3
Reporter: Peter Assignee: John Stewart (Inactive)
Resolution: Done Votes: 3
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate

 Description   

In the old api i had to make frequently casts like this

BasicDBList points = ((BasicDBList) ((BasicDBObject) currentObject.get("poly")).get("coordinates"));

If the Document class had functions like this

Document getDocument(String key); <- sub document
List<Document> getDocumentArray(String key); <- Array of sub documents
List<Object> getArray(String key); <- Array of other objects

i could avoid such casts



 Comments   
Comment by Githook User [ 02/Feb/19 ]

Author:

{'name': 'John Stewart', 'email': 'john.stewart@mongodb.com', 'username': 'jstewart-mongo'}

Message: Add getList methods to the Document class

JAVA-1720
Branch: 3.10.x
https://github.com/mongodb/mongo-java-driver/commit/dc9e69b0f91a466300772f7ade81134f3b85be49

Comment by Githook User [ 31/Jan/19 ]

Author:

{'name': 'John Stewart', 'email': 'john.stewart@mongodb.com', 'username': 'jstewart-mongo'}

Message: Add getList methods to the Document class

JAVA-1720
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/b08e7cf3aeb6077dbfda62b2aeed8fc2417ca663

Comment by Peter [ 13/Mar/17 ]

@Jeff ... for single fields the new API is ok, but for Arrays we still have to cast each time, i think it would be handy if

public <T> T get(final Object key, final Class<T> clazz) ;

would have a according function

public <T> List<T> getList(final Object key, final Class<T> clazz) ;

Comment by Koen [X] [ 13/Mar/17 ]

The current drivers supports List<Document> list = document.get("people", List.class), but it tends to display warnings related to unchecked casting. You also can't use this structure when chaining such as in document.get("people", List.class).get("name") since get("name") is not available in Object.

I would recommend

List<T> document.getList(String name, Class<T> type);

This way you can use the same method to get objects with getList("people", Document.class) :

"people" : [ {"name" : "sample"} ]

And arrays of simple types with getList("words", String.class)

"words" : ["sample"]

Whether you name it getList or getArray I leave up to you. From a Java point of view I would prefer List since I expect getList to return a List and getArray to return an array.

Comment by Ross Lawley [ 17/May/16 ]

New pull request for this feature: https://github.com/mongodb/mongo-java-driver/pull/351

Comment by Jeffrey Yemin [ 16/Jul/15 ]

As we haven't heard back from you, I'm going to close this issue, but please feel free to comment and we can re-open if necessary.

Comment by Jeffrey Yemin [ 23/Jun/15 ]

Just checking whether the Document.get(String, key, Class<T> clazz) method meets your needs.

Regards,
Jeff

Comment by Jeffrey Yemin [ 26/Mar/15 ]

You can also currently do:

        Document doc = new Document().append("listKey", Arrays.asList(1, 2, 3)).append("docKey", new Document("a", 1));
 
        List<Integer> list = doc.get("listKey", List.class);
        Document nestedDoc = doc.get("docKey", Document.class);
 
        for (int i : list) {
            System.out.println(i);
        }
 
        System.out.println(nestedDoc);

Definitely susceptible to runtime errors, but there are no casts.

Comment by Jeffrey Yemin [ 26/Mar/15 ]

I don't love the List<Object>, since you'll still have a cast. But I agree that we should do something.

Have you had a look at the org.bson.BsonDocument class in 3.0? It's a bit verbose (no implicit conversions in Java, alas), but it has the advantage of complete type safety and no casts. If you prefer it, you can use it instead of Document.

Generated at Thu Feb 08 08:55:19 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.