I have a collection of documents structured like this. For instance:

{ 'observer': 'machine1', 
  'seen': [ 
     {'page1': ['/link1', '/link3']},
     {'page2': ['/link4', '/link1']},
}

{ 'observer': 'machine2', 
  'seen': [ 
     {'page3': ['/link2']},
     {'page1': ['/link5']},
}

I'm trying to get a list of all distinct keys and values in the array, grouped by observer. In an ideal world, it would look something like:

{'machine1': ['/link1', '/link3', '/link4'], 'machine2': ['/link2', '/link5'] }

and

{'machine1': ['page1', 'page2'], 'machine2': ['page1', 'page3']}

I understand that I can use $aggregate and $group to get unique values based on sublists, but I'm unsure how to deal with the lists of objects and grab their keys and values.

1 Answers

1
Anthony Winzlet On Best Solutions

You can use below aggregation

db.collection.aggregate([
  { "$unwind": "$seen" },
  { "$addFields": {
    "seen": {
      "$objectToArray": "$seen"
    }
  }},
  { "$unwind": "$seen" },
  { "$unwind": "$seen.v" },
  { "$group": {
    "_id": "$observer",
    "links": { "$addToSet": "$seen.v" }
  }}
])

MongoPlayground

If you need pages and links simultaneously

db.collection.aggregate([
  { "$unwind": "$seen" },
  { "$addFields": {
    "seen": {
      "$objectToArray": "$seen"
    }
  }},
  { "$unwind": "$seen" },
  { "$unwind": "$seen.v" },
  { "$facet": {
    "pages": [
      { "$group": {
        "_id": "$observer",
        "pages": { "$addToSet": "$seen.v" }
      }}
    ],
    "links": [
      { "$group": {
        "_id": "$observer",
        "links": { "$addToSet": "$seen.k" }
      }}
    ]
  }}
])

MongoPlayground

If you need some more enhancement

db.collection.aggregate([
  { "$unwind": "$seen" },
  { "$addFields": {
    "seen": {
      "$objectToArray": "$seen"
    }
  }},
  { "$unwind": "$seen" },
  { "$unwind": "$seen.v" },
  { "$facet": {
    "pages": [
      { "$group": {
        "_id": "$observer",
        "pages": { "$addToSet": "$seen.v" }
      }}
    ],
    "links": [
      { "$group": {
        "_id": "$observer",
        "pages": { "$addToSet": "$seen.k" }
      }}
    ]
  }},
  { "$project": {
    "pages": {
      "$map": {
        "input": "$pages",
        "in": {
          "$let": {
            "vars": {
              "links": {
                "$arrayElemAt": [
                  "$links",
                  { "$indexOfArray": ["$links._id", "$$this._id"] }
                ]
              }
            },
            "in": {
              "_id": "$$this._id",
              "pages": "$$this.pages",
              "links": "$$links.links"
            }
          }
        }
      }
    }
  }},
  { "$unwind": "$pages" },
  { "$replaceRoot": { "newRoot": "$pages" }}
])

MongoPlayground