MongoDB
 sql >> Base de Dados >  >> NoSQL >> MongoDB

grupo mongodb pelo primeiro caractere


Você não mostra a aparência dos documentos em sua coleção, mas pode usar o aggregate método de coleta para fazer isso:
// 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}}})