Versuchen Sie, die folgende Aggregationsanfrage:
db.users.aggregate([{$project: { _id: 0, name: { $substr: [ "$name", 0, 1 ]}}}])
Dies nutzt die Projektionen und Teilzeichenfolge: https://docs.mongodb.com/v3.2/reference/operator/aggregation/project/ https://docs.mongodb.com/v3.2/reference/operator/aggregation/substr/
Zum Beispiel:
> db.dropDatabase()
{ "dropped" : "test", "ok" : 1 }
> db.users.save({"name": "Kevin"})
WriteResult({ "nInserted" : 1 })
> db.users.save({"name": "Matt"})
WriteResult({ "nInserted" : 1 })
> db.users.save({"name": "Sakis"})
WriteResult({ "nInserted" : 1 })
> db.users.save({"name": "Lee"})
WriteResult({ "nInserted" : 1 })
>
> db.users.aggregate([{$project: { _id: 0, name: { $substr: [ "$name", 0, 1 ]}}}])
{ "name" : "K" }
{ "name" : "M" }
{ "name" : "S" }
{ "name" : "L" }
Scheck ** [$ substr] (https : //docs.mongodb.com/v3.2/reference/operator/aggregation/substr/) ** – Yogesh
... und ['$ redact'] (https://docs.mongodb.com/manual/reference/operator/aggregation/redact/) – styvane