【发布时间】:2020-07-27 18:14:23
【问题描述】:
我有以下 _id: userID 格式的对象和一个 Mongoose 查询:
const obj = {
111: "222",
333: "444",
555: "666"
};
Model.aggregate([
{
$addFields: {
userID: ???
}
}
]);
我需要一种基于对象向每个文档添加userID 字段的方法。因此,假设该集合具有以下数据:
[
{
_id: "111",
...
},
{
_id: "333",
...
},
{
_id: "555",
...
}
]
文档最终应该是这样的:
[
{
_id: "111",
userID: "222",
...
},
{
_id: "333",
userID: "444",
...
},
{
_id: "555",
userID: "666",
...
}
]
我该怎么做?
【问题讨论】:
标签: javascript node.js mongodb mongoose aggregation-framework