我正在使用猫鼬在集合中(向每个数据)添加一个字段。它返回零错误,但是,当我检查数据库时,它不添加任何字段。
app.use('/', (res, req) => {
mongoose.connection.db.collection('placemodels').aggregate(
[
{
$addFields: {
type: 'place',
},
},
],
(err, e) => {
console.log(err, 'ahsidufhieuahiu');
console.log(e);
}
);
});
我在哪里弄错了?
编辑
mongoose.connection.db.collection('placemodels').aggregate(
[
{
$set: {
type: 'place',
},
},
],
(err, e) => {
console.log(err, 'ahsidufhieuahiu');
console.log(e);
}
);
答案 0 :(得分:1)
$addFields
流水线阶段只会将新字段添加到已获取的文档中,即它将永远不会实际修改数据库中的文档。
换句话说-它只修改输出中的文档。
如果您想向数据库中的文档中实际添加新字段,则可以使用collection.update(..)
和$set
运算符:https://docs.mongodb.com/manual/reference/operator/update/set/