[JAVA-2532] Improve the Document class in Java to allow referencing of embedded documents using the dot notation. Created: 08/Jun/17 Updated: 29/Oct/23 Resolved: 01/Feb/19 |
|
| Status: | Closed |
| Project: | Java Driver |
| Component/s: | BSON |
| Affects Version/s: | None |
| Fix Version/s: | 3.10.0 |
| Type: | New Feature | Priority: | Major - P3 |
| Reporter: | Andrew Harris | Assignee: | John Stewart (Inactive) |
| Resolution: | Fixed | Votes: | 1 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Issue Links: |
|
||||||||
| Description |
|
Currently the Document class does not support the dot notation when referencing embedded documents. It would be very useful if it did. |
| Comments |
| Comment by Githook User [ 02/Feb/19 ] | ||||||||
|
Author: {'name': 'John Stewart', 'email': 'john.stewart@mongodb.com', 'username': 'jstewart-mongo'}Message: Update DocumentSpecification unit tests
| ||||||||
| Comment by Githook User [ 02/Feb/19 ] | ||||||||
|
Author: {'name': 'John Stewart', 'email': 'john.stewart@mongodb.com', 'username': 'jstewart-mongo'}Message: Add getEmbedded methods for extracting values in embedded documents
| ||||||||
| Comment by Githook User [ 01/Feb/19 ] | ||||||||
|
Author: {'name': 'John Stewart', 'email': 'john.stewart@mongodb.com', 'username': 'jstewart-mongo'}Message: Update DocumentSpecification unit tests
| ||||||||
| Comment by Githook User [ 01/Feb/19 ] | ||||||||
|
Author: {'name': 'John Stewart', 'email': 'john.stewart@mongodb.com', 'username': 'jstewart-mongo'}Message: Add getEmbedded methods for extracting values in embedded documents
| ||||||||
| Comment by Andrew Harris [ 20/Dec/17 ] | ||||||||
|
Ok but then have a dot in a name seems like an anti-pattern to me and although allowed, should be discouraged especially as a dot notation to traverse levels of embedded documents is used in the Mongo shell (and other places too). Surely this would cause confusion at best but more likely a syntax and/or semantic conflict? All I'm suggesting is that this is carried forward to the Java API itself. | ||||||||
| Comment by Jeffrey Yemin [ 08/Dec/17 ] | ||||||||
|
The restriction against dots in field names is only for documents that are stored in MongoDB collections, and that restriction is likely to be relaxed in the future. MongoDB query and update documents, in contrast, can contain dots, e.g.
| ||||||||
| Comment by Andrew Harris [ 08/Dec/17 ] | ||||||||
|
I can see your point but you cannot have dots in field names so your edge case concern isn't valid I think. if you try and do db.collection.insert( {"a.b":"1"}) then it throws an error as below "Error: can't have . in field names [a.b] :" | ||||||||
| Comment by Jeffrey Yemin [ 26/Oct/17 ] | ||||||||
|
Hi Andrew. It's a bit of an edge case, but I'm concerned about something like this:
It would be a bit more verbose, but perhaps getDotted could take a list of field names, e.g.
| ||||||||
| Comment by Andrew Harris [ 26/Oct/17 ] | ||||||||
|
Surely a getDotted("") method that recursively returns the field value would be a solution that doesn't break existing interfaces, is still Map valid and still allows the use of dots in field names (which is a weird use case that is asking for trouble imho). | ||||||||
| Comment by Andrew Harris [ 13/Jun/17 ] | ||||||||
|
I've written my own wrapper class around the Document class that does just what I propose but it would good if Document supported this natively. |