Problem
Given this structure:
{
"_id": ObjectId("56n606c39q49b80adfe6d17b")
"data": {
"field1": [
{
"subfield1.1": [
{
"val1.1.1": a
},
{
"val1.1.2": b
}
]
},
{
"subfield1.2": [
{
"val1.2.1": c
},
{
"val1.2.2": d
}
]
}
]
}
}
How can I write a query that counts the number of documents that have a maximum unique "val" considering all the "val"s in every "subfield" within a given field?
Facts to consider:
- "val"s are elements in an array
- "subfield"s are also an elements in an array
- "field"s, "subfield"s and "val"s field names are the same for all documents
- There could be 1 or many "val"s
I am somewhat new to NoSQL. In normal SQL I'd approach this problem maybe with a self join, but here, even though it's possible to use Aggregation, I couldn't find a way to even get close to the real solution.
Case Examples
Given that a is the maximum value... This document SHOULD be counted:
{
"_id": ObjectId("56n606c39q49b80adfe6d17b")
"data": {
"field1": [
{
"subfield1.1": [
{
"val1.1.1": a
},
{
"val1.1.2": b
}
]
},
{
"subfield1.2": [
{
"val1.2.1": c
},
{
"val1.2.2": d
}
]
}
]
}
}
This document SHOULDN'T be counted:
{
"_id": ObjectId("56n606c39q49b80adfe6d17b")
"data": {
"field1": [
{
"subfield1.1": [
{
"val1.1.1": a
},
{
"val1.1.2": a
}
]
},
{
"subfield1.2": [
{
"val1.2.1": b
},
{
"val1.2.2": c
}
]
}
]
}
}
This document SHOULDN'T be counted either:
{
"_id": ObjectId("56n606c39q49b80adfe6d17b")
"data": {
"field1": [
{
"subfield1.1": [
{
"val1.1.1": a
},
{
"val1.1.2": b
}
]
},
{
"subfield1.2": [
{
"val1.2.1": a
},
{
"val1.2.2": c
}
]
}
]
}
}
This document SHOULD be counted (even if b is repeated):
{
"_id": ObjectId("56n606c39q49b80adfe6d17b")
"data": {
"field1": [
{
"subfield1.1": [
{
"val1.1.1": a
},
{
"val1.1.2": b
}
]
},
{
"subfield1.2": [
{
"val1.2.1": b
},
{
"val1.2.2": c
}
]
}
]
}
}
Any idea would be welcomed. Thank you!