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

grupo mongodb por primer carácter

No muestra cómo se ven los documentos en su colección, pero puede usar el aggregate método de recopilación para hacer esto:

// Group by the first letter of the 'words' field of each doc in the 'test'
// collection while generating a count of the docs in each group.
db.test.aggregate({$group: {_id: {$substr: ['$words', 0, 1]}, count: {$sum: 1}}})