How to present the results of the aggregation as an array of strings (values) and not as a BSON object with a key and and a value?

Hi guys,

I have an aggregation pipeline query which outputs a BSON object with a key-value pair, value being an array of strings:

db.dataGroupSchema.aggregate([
   {
      "$project":{
         "_id":1,
         "dataSourceId":"$dataSourceIds.value"
      }
   },
   {
      "$unwind":"$dataSourceId"
   },
   {
      "$group":{
         "_id":"$dataSourceId",
         "count":{
            "$sum":1
         }
      }
   },
   {
      "$match":{
         "count":{
            "$gt":0
         }
      }
   },
   {
      "$group":{
         "_id":"",
         "duplicatedDataSourceIds":{
            "$push":"$_id"
         }
      }
   },
   {
      "$project":{
         "duplicatedDataSourceIds":1,
         "_id":0
      }
   }
]);

output:

{
“duplicatedDataSourceIds” : [
“a75de0d0-df16-4618-9d3e-73c59b5137a5”,
“3a83da20-3524-4fb7-aeda-26e1daadc214”,
“450fdceb-13f1-4e4d-9ac7-4890b35268f1”,
“409c68ea-d600-42f5-ab3f-d29aac86fc68”
]
}

I want to reuse this array as a variable so I want just an array of strings without a key and not in an object. Similar to the result of the distinct():

db.dataGroupSchema.distinct("dataSourceIds.value")

[
“3a83da20-3524-4fb7-aeda-26e1daadc214”,
“409c68ea-d600-42f5-ab3f-d29aac86fc68”,
“450fdceb-13f1-4e4d-9ac7-4890b35268f1”,
“a75de0d0-df16-4618-9d3e-73c59b5137a5”
]

Is there an option to do it in plain vanilla mongo script?

Mongo shell is javascript. In JS, to get a given field, you simply access the field, just like you do for any JS object. For example,

mongosh> document = {
  "duplicatedDataSourceIds" : [
    "a75de0d0-df16-4618-9d3e-73c59b5137a5",
    "3a83da20-3524-4fb7-aeda-26e1daadc214", 
    "450fdceb-13f1-4e4d-9ac7-4890b35268f1",
    "409c68ea-d600-42f5-ab3f-d29aac86fc68"
]
}
mongosh> array = document.duplicatedDataSourcesIds
/* output is */
[
  'a75de0d0-df16-4618-9d3e-73c59b5137a5',
  '3a83da20-3524-4fb7-aeda-26e1daadc214',
  '450fdceb-13f1-4e4d-9ac7-4890b35268f1',
  '409c68ea-d600-42f5-ab3f-d29aac86fc68'
]

Thanks for your reply @steevej. I am not sure how to implement it:

The function aggregate return a cursor, not an object.

Something like the following should work:

/* Store all the documents from the results cursor */
all_results = results.toArray()
/* Get the first document */
first_result = all_results[0]
/* Get the array of strings */
array = first_result.duplicatedDataSourceIds

Please read Formatting code and log snippets in posts before next post so that we can cut-n-paste your code.

I hope you are safe.

1 Like

Thank you, @steevej!

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.