[GODRIVER-1707] Inbuilt Support For Getting Keys in a Sub Document. Created: 05/Aug/20  Updated: 17/Aug/20  Resolved: 17/Aug/20

Status: Closed
Project: Go Driver
Component/s: API
Affects Version/s: 1.5.0
Fix Version/s: None

Type: New Feature Priority: Major - P3
Reporter: Shantanu Bansal Assignee: Divjot Arora (Inactive)
Resolution: Won't Do Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

Hi, We should support an inbuilt capability in the mongo driver to fetch the distinguished keys in a sub-document
For Ex:
"nodes" :

{ "master" : \{ ... }

,
"worker" : { ... }
So the functionality should work like. If I provide the path to nodes. It should give me all the keys in a tree structure.

this will be very handy just to have keys from the driver it self.



 Comments   
Comment by Divjot Arora (Inactive) [ 17/Aug/20 ]

shantanubansal05@gmail.com Glad to hear that you have this working in your application. As I mentioned before, we don't think this is a very common use case and are therefore going to hold off on adding it to the driver right now. I'm going to close this ticket, but we can revisit and potentially open it again if we see this use case come up again from other users.

– Divjot

Comment by Shantanu Bansal [ 16/Aug/20 ]

Hi @Divjot,
Yep! I too did something in my code. But thought to give this as suggestion as people may find it useful if it comes out if the box.
but np thankyou so much for taking the time and reading the ticket.

Comment by Divjot Arora (Inactive) [ 14/Aug/20 ]

I don't think this is something we want to add to the driver's public API because it's not a common use case and users will have different requirements. Some may want a list of only top-level keys. Others might want a list of all keys, where nested ones are in the format "topLevelKey.nestedKey". I've written up some code using FindOne to do this that you can migrate to your application: https://gist.github.com/divjotarora/27dfabb358771f526f661a9ec79f9f43. The one thing I noticed is that our bson.Raw type has a method to get a slice of Elements, which are key/value pairs, and a method to get just  a slice of values, but not one to get a slice of keys, so there's an extra loop required in the code to get the keys from the elements. If you think it would help reduce complexity a bit, I can file a ticket to add a bson.Raw.Keys method.

– Divjot

Comment by Shantanu Bansal [ 14/Aug/20 ]

It depends on how we want to do it. The approach should be optimized. 

Q.). Do you want to get a list of keys from a specific document in a collection?
Ans: It can be a document in a collection or sub-document.
The queried thing should be at the document level not at the whole collection level. It will be implemented with FindOne.

Q.).  would it be reasonable to first run Collection.Find or Collection.FindOne to get the document and then creating a slice of keys in the application/driver or does your use case require getting a list of keys directly from the server?
Ans: Yes, this is one approach. But if server internally support to fetching keys efficiently than why not do there.

Comment by Divjot Arora (Inactive) [ 14/Aug/20 ]

Hi shantanubansal05@gmail.com,

I'd like to get a few more details before I make suggestions. Do you want to get a list of keys from a specific document in a collection? Or a list of keys of documents? If it's for a specific document, would it be reasonable to first run Collection.Find or Collection.FindOne to get the document and then creating a slice of keys in the application/driver or does your use case require getting a list of keys directly from the server?

– Divjot

 

Comment by Shantanu Bansal [ 14/Aug/20 ]

Hi @Divjot,

What I mean is we should be able to fetch the keys in the document or sub-document. 

 

 

{
"key1": "value1",
"key2": "value2",
"key3" : {
           "subKey1:":"subValue1",
         }
}

The abstraction should return me:

If I search at the root level:

 

[
"key1", "key2", "key3"
]

if queried at key3

["subKey1"]

 

 

 

Same goes for Values

Comment by Divjot Arora (Inactive) [ 10/Aug/20 ]

Hi shantanubansal05@gmail.com,

I didn't quite follow the request in the description. Can you provide a complete minimal example of a stored document and what information you'd like to get from it? If possible, it'd also be helpful to know which of the driver's currently exported methods you'd like to use to get this information.

– Divjot

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