我正在使用express-restify-mongoose
库来拥有其他终结点反对猫鼬。
我有架构看起来像这样:
const BookSchema = new Schema(
{
name: { type: String },
items: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Book' }],
}
);
因此,我发送了http补丁请求:
{ name: 'blabla' }
,它按预期更改了名称。但是,当我想将项目添加到像
{ items: ["5dd138199f6ecb3990360328"] }
这样的项目数组时,将其替换为整个对象(用一个5dd138199f6ecb399036032d
项目)。在挖掘源代码I see here之后,该函数将使用
findOneAndUpdate
和$set
。所以我的问题是,有没有办法使用
$push
或$set
值中的任何函数/属性?我无法添加到该库中,但也许这里有任何解决方法?
最佳答案
我认为猫鼬中最接近的解决方案是使用Set Elements in Arrays:
"items.1": "5dd138199f6ecb3990360355"
它将添加到数组,但您必须传递位置。
关于node.js - 如何在 Mongoose 的$ set内使用$ push?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58900577/