Given these documents:
{
values: [
{ attribute: 1 },
{ attribute: 2 },
{ attribute: 3 },
{ attribute: 4 },
]
},
{
values: [
{ attribute: 2 },
{ attribute: 3 },
{ attribute: 4 },
]
},
{
values: [
{ attribute: 2 },
{ attribute: 3 },
]
}
I'm trying to get the common "attribute" values:
[ 2, 3 ]
I'm looking at the aggregator framework, but I've found nothing that could really answer my need for now.
I'm using Mongo 2.4.6.
Thanks in advance for your answers!
EDIT
In fact, my documents can have duplicated attributes (but I want to count them only once per document).
Given this data
{
values: [
{ attribute: 1 },
{ attribute: 2 },
{ attribute: 3 },
{ attribute: 3 },
{ attribute: 4 },
]
},
{
values: [
{ attribute: 2 },
{ attribute: 2 },
{ attribute: 3 },
{ attribute: 4 },
]
},
{
values: [
{ attribute: 2 },
{ attribute: 3 },
]
}
Then the query should return:
{
"result" : [
{
"values" : 2
},
{
"values" : 3
}
],
"ok" : 1
}
Anand, the query you posted will count attribute "2" 4 times, instead of 3 times. I tried to modify it, but this is still quite cryptic to me...
Thanks in advance.