Get keys of fields from Embedded Array of Documents Mongodb Java
Get keys of fields from Embedded Array of Documents Mongodb Java
Here is a simplified schema of my MongoDB doc:
"_id": 0,
"config": [
"property1": "a",
"property2": "b",
"property3": "c",
"property4": "d"
,
"property1": "a",
"property2": "bb",
"property3": "cc",
"property4": "d",
"ispropert5": true
,
"property1": "a",
"property2": "b",
"property3": "c",
"property4": "ddd",
"ispropert5": false,
"ispropert6": false
],
"entity": "123asdf",
"url": "",
"createdDate": 1
As an output I need to get a list of unique keys of nested documents:
property1, property2, property3, property4, ispropert5, ispropert6
I am trying this in my class but of course failing to cast ArrayList to Document:
Document dbo = col.find().first();
Set<String> keys = dbo.keySet();
Iterator iterator = keys.iterator();
while(iterator.hasNext())
String key = iterator.next().toString();
if(dbo.get(key) instanceof ArrayList)
Document dboNested = (Document) dbo.get(key); //java.lang.ClassCastException: java.util.ArrayList cannot be cast to org.bson.Document
Set<String> keysNested = dboNested.keySet();
System.out.println("KeyNested: " + keysNested);
3 Answers
3
Like you've commented, on your code, you cannot convert ArrayList to Document, therefore you must cast it to an ArrayList like below:
while(iterator.hasNext())
String key = iterator.next().toString();
Object value = dbo.get(key);
if (value instanceof ArrayList)
ArrayList<?> dboArrayNested = (ArrayList<?>) value;
for (Object dboNestedObj : dboArrayNested)
if (dboNestedObj instanceof Document)
printKeysNested(Document.class.cast(dboNestedObj));
// else if extra...
else if (value instanceof Document)
printKeysNested(Document.class.cast(value));
public static void printKeysNested(Document dboNested)
Set<String> keysNested = dboNested.keySet();
System.out.println("KeyNested: " + keysNested);
I hope this helps you.
I guess i am late here, but i was trying a more mongo way to do this. You would definitely want to execute this on a small set of query, may be a match
stage before even starting this
match
db.coll.aggregate([
$unwind: '$config'
,
$project:
arr:
$objectToArray: '$config'
,
$unwind: '$arr'
,
$group:
_id:
key: '$arr.k'
,
$project:
key: '$_id.key',
_id: 0
])
thanks so much @Avij, I was fighting in mongo shell to build a right query and you did it! works perfectly.
– Katsiaryna Piotukh
Sep 2 at 9:00
please upvote if it helped you
– Avij
Sep 2 at 10:20
thanks to all the answers, here is the final code I have:
while (iterator.hasNext())
String key = iterator.next().toString();
Object value = dbo.get(key);
if (value instanceof ArrayList)
ArrayList<?> dboArrayNested = (ArrayList<?>) value;
for (Object dboNestedObj : dboArrayNested)
Document dboNested = Document.class.cast(dboNestedObj);
if (dboNestedObj instanceof Document)
keysNested.addAll(dboNested.keySet());
// else if extra...
else if (value instanceof Document)
Document dboNested = Document.class.cast(value);
keysNested.addAll(dboNested.keySet());
System.out.println("KeysNested: " + keysNested);
this outputs for the given example:
[property1, property2, property3, property4, ispropert5, ispropert6]
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
thanks a lot @Alvaro, thats work!!! I see now I had to loop thru ArrayList. Just this prints the list of keys for each nested document. I a little bit modified the code, so I get the list of unique keys as one set.
– Katsiaryna Piotukh
Sep 2 at 9:50