3
Hello, I have a collection that has the documents with the format below:
{
"_id" : ObjectId("..."),
"checked": true,
"styles": ["foo", "bar", "bin"]
}
I need to add the array size styles
for all documents that meet a filter, ex:
db.test.find({ checked: { $exists : true } })/* aqui entraria o resto da query*/
How should be the query so I can generate this sum (in Mongodb). If I were to do after returning the result I believe I would have a loss of performance. We can say that it would be something like the code below after the return:
function getData(filter) {
/* retorna o resultado esperado*/
}
var soma = getData({checked:true})
.map(x=>x["styles"].length)
.reduce((a,b) => a+=b, 0)
/* soma representaria a soma do tamanho da propriedade styles */
How to do?
but I don’t want to know the number of documents, I want the sum of the field size
styles
that exists in each document. In the example I have 1 document with 3 items in the field, then the result will be 3– LeandroLuk
This field is an array? or an object?
– XTRADE XTRADE
the Styles field is an array
– LeandroLuk