How to get top object names from document


#1

How to get top 3(dynamic) object names

{
“type”: “versions”,
“data”: {
“1.0”: [
“1.0.1”,
“1.0.2”
],
“1.1”: [
“1.1.1”,
“1.1.2”
],
“1.3”: [
“1.3.1”,
“1.3.2”
],
“1.4”: [
“1.4.1”,
“1.4.2”
]
}
}

expected output

[“1.0”,“1.1”,“1.3”]


#2

Hi @venkata84.joga, I’m not sure what SDK you’re using, but if it’s Java then this should do it:

     List<String> top = bucket.get("docId")
            .content()
            .getObject("data")
            .getNames()
            .stream()
            .filter(s -> s.matches("[0-9]+\\.[0-9]+"))
            .map(Double::parseDouble)
            .sorted()
            .limit(3)
            .map(Object::toString)
            .collect(Collectors.toList());

#3

This function give limit but not the top values

i need out put in descending order.


#4
SELECT ARRAY v FOR pos:v IN OBJECT_NAMES(d.data) WHEN pos < 3 END
FROM default d
WHERE type = "versions";

#5

Thanks for you response . Can we sort in desc order and then can get top 3 values? Like below

[“1.4”,“1.3”,“1.2”]


#6

@venkata84.joga the code I gave was to give your original expected output, which was ascending. If you want descending, just change the .sorted() to .sorted(Collections.reverseOrder()).


#7
SELECT  (SELECT RAW v1 
                  FROM OBJECT_NAMES(d.data) AS v1
                 ORDER BY v1 DESC 
                 LIMIT 3) AS vers
FROM default d 
WHERE type = "versions";