我正在寻找从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: []
}
}
我想要做的是从path1
移除boardItems
并将其推送到undoedItems
$pull
删除我正在使用以下内容从path1
删除boardItems
。
注意:以下内容属于我的roomSchema.methods
,因此this
引用了Room
的特定roomSchema
个实例
// 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
}
]
}
});