mongoose拉和得到拉项目
我正在从Mongoose的嵌套数组中删除一个项目,并将其返回,以便我可以将其推送到我的对象中的另一个数组。
以下是Room
模型的一个实例:
{ name: "Foo", boardBucket: { currentBoardId: 1234, items: [ { boardItems: [ { id: 1, data: "path1" }, { id: 2, data: "path2" } ] }, { boardItems: [ { id: 3, data: "path4" }, { id: 4, data: "path5" } ] } ], undoedItems: [] } }
我想要做的是从boardItems
删除path1
,并将其推入到undoedItems
使用$pull
删除
我正在使用以下从boardItems
删除path1
。
注意:下面是我的roomSchema.methods
所以this
是指roomSchema.methods
一个特定的Room
实例
// where `i` is the index of the "board" I'd like to operate // on and `itemId` the `id` of the path I want to pull // - Using this dot notation because of nested array // - Note that `${i}` is simply an ES6 interpolated template // literal that has nothing to do with mongo's positional // operator var path = `boardBucket.items.${i}.boardItems`; var updateOp = {}; updateOp[path] = { id: itemId }; this.update({ $pull: updateOp }, (err, result)=> { if (err) throw err; console.log(result); });
这个问题
我怎样才能得到“拉”项目,所以我可以$push
它到undoed
项目?
roomSchema
如果有帮助,这是我的roomSchema
var roomSchema = new mongoose.Schema({ name: String, boardBucket: { currentBoardId: String, items: [ { active: Boolean, boardId: String, boardItems: Array, undoedItems: Array, viewPosition: String } ] } });