[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: |
|
||||
| Description |
|
In the old api i had to make frequently casts like this
If the Document class had functions like this
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
| ||||||||||
| 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
| ||||||||||
| 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
would have a according function
| ||||||||||
| 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
This way you can use the same method to get objects with getList("people", Document.class) :
And arrays of simple types with getList("words", String.class)
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, | ||||||||||
| Comment by Jeffrey Yemin [ 26/Mar/15 ] | ||||||||||
|
You can also currently do:
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. |