sql >> Base de Datos >  >> NoSQL >> MongoDB

MongoDB:cuente la cantidad de elementos en una matriz

En MongoDB 2.6, Aggregation Framework tiene una nueva matriz $size operador que puede utilizar:

> db.mycollection.insert({'foo':[1,2,3,4]})
> db.mycollection.insert({'foo':[5,6,7]})

> db.mycollection.aggregate([{$project: { count: { $size:"$foo" }}}])
{ "_id" : ObjectId("5314b5c360477752b449eedf"), "count" : 4 }
{ "_id" : ObjectId("5314b5c860477752b449eee0"), "count" : 3 }