【发布时间】:2023-04-08 18:39:01
【问题描述】:
在我的 node.js 应用程序中,我使用 mongodb 和 mongoose 驱动程序。如何重塑 Modell.find() 操作的结果?如果我得到这样的示例文档:
{
_id: ObjectId(...),
score: 14,
time: 123
}
在我的查找操作中,我想在 .find() 的结果上添加一个索引(计数器)并重命名 _id 字段。这可能吗?
如果希望我的文档流看起来像这样:
[{
index: 0
player: ObjectId // _id field renamed to player
score: 14,
time: 123
},
{
index: 1
player: ObjectId // _id field renamed to player
score: 6,
time: 321
},
...
{
index: N
player: ObjectId // _id field renamed to player
score: 1,
time: 456
}
]
我试过了
Modell.find({}, {player: '$_id', score: 1, time: 1})
.exec(function(err, players) {
...
});
但 _id 字段在生成的文档流中未重命名。这可能吗?以及如何在文档流中创建文档计数器。
【问题讨论】:
标签: javascript node.js mongodb mongoose aggregation-framework