1

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 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I'm quite new to Mongo so I didn't have a good sense of how unwind and facet work together. This is very helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.