猫鼬拉动并获得拉动物品

时间:2016-08-18 04:05:41

标签: node.js mongodb 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: []
  }
}

我想要做的是从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
        }
      ]
    }
});

0 个答案:

没有答案