$project / $addField 字段名称(路径)包含 .或$

问题描述 投票:0回答:1

我在使用 mongodb 投影某些字段时遇到问题

$project

我的收藏有以下数据

[{num:"SI-0101",mob:6289141515},...]

和我的

$project
查询

[{
  $project: {
    "Id No.": "$num",
    "Mobile No.": "$mob" // note on that ., i need it in o/p
  }
}]

当我执行此操作时,它会引发错误:

Field path not ends with .

我该如何解决这个问题?

mongodb mongodb-query aggregation-framework
1个回答
0
投票

MongoDb 不支持以点结尾的键 (

.
)。您可以在查询后在代码中对其进行编辑。例如:

const userRes = await userModel.findOne({num:"SI-0101"}, {num: 1, mob: 1}).lean();
return {'Id No.': userRes.num, 'Mobile No.': userRes.mob}
© www.soinside.com 2019 - 2024. All rights reserved.