[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:
Duplicate
duplicates JAVA-260 Support dot notation in DBObject.get ... Closed

 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

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

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

JAVA-2532
Branch: 3.10.x
https://github.com/mongodb/mongo-java-driver/commit/996a5eb7a28d5100f198a9a788b7aeb65d2adede

Comment by Githook User [ 01/Feb/19 ]

Author:

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

Message: Update DocumentSpecification unit tests

JAVA-2532
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/305d3e5fe9e4903c6ecf7761aea8c4487d78f508

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

JAVA-2532
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/271da0549d17d3d084cb7c2af2a606f9dc6559d3

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.

    collection.find(new Document("a.b", 1))

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:

        Document doc = new Document()
                .append("a", new Document("b", 1)
                .append("a.b", 2));
        doc.getDotted("a.b"); // does it return 1 or 2?

It would be a bit more verbose, but perhaps getDotted could take a list of field names, e.g.

        Document doc = new Document().append("a", new Document("b", 1)
                .append("a.b", 2));
        doc.getDotted(List.of("a", "b"));   // returns 1
        doc.getDotted(List.of("a.b"));      // returns 2

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.

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