Hey everyone. I have a trouble of aggregation of heterogeneous objects from the one collection. For example:
{
"id": "google.com/q=blah",
"type": "url",
"domain": "google.com"
"key_id": 123
},
{
"id": "google.com/q=blah",
"domain": "google.com"
"type": "url",
"key_id": 321
},
{
"id": "google.com"
"type": "domain",
"key_id": 123,
"registred": True,
},
{
"id": "google.com"
"type": "domain",
"key_id": 321,
"registred": True,
}
My intention here is to merge objects that have the same “id” objects:
$group: {
_id: "$id",
"key_ids": {"$addToSet": "$key_id"},
"type": {"$last": "$type"}
"domain": {"$last": "$domain"}
},
$project: {
"id": "$_id",
"keys_ids": "$key_ids",
"type": "$type",
"domain": "$domain"
}
This strategy works well when the query is filtered by “type” field and all objects have the same field set.
When I have in the query output multiple types with different set of fields I don’t know how do I check if field exists and add it to project.
Is it possible generally with Mongo?