来自Set的Mongoose $ pullAll

时间:2018-04-28 11:23:07

标签: mongoose mongoose-schema

鉴于以下场景Schema,实际上已添加Dasbhoards以正确设置upsert。 我试图以没有成功来拉动所有。

/**
 * Dashboard Schema
 */
const DashboardSchema = new mongoose.Schema({
  _user: { type: Schema.Types.ObjectId,
    ref: 'User',
    required: true,
    unique: false,
  },
  _cards: [{
    _id: false,
    cardId: { type: Schema.Types.ObjectId },
    cardType: {
      type: String,
      enum: ['BATTLE', 'QUEST', 'MISSION'],
    },
  }],
});

And Statics

/**
* DASHBOARD_STATICS
**/
pullAndUpdate(dashboardId, cards) {
return this.findOneAndUpdate(
  {
    _id: dashboardId,
  },
  {
    $pullAll: { _cards: [cards] },
  },
  {
    upsert: false,
    setDefaultsOnInsert: false,
    new: true,
  },
  )
.exec()
  .then((Dashboard) => {
    if (Dashboard) {
      return Dashboard;
    }
    const err = new Error('Error Upserting Dashboard', 
    httpStatus.NOT_FOUND);
    return Promise.reject(err);
    });
  },
};

` pullAndUpdate func中的card param正在获取像

这样的对象数组
{
  "cardId":ObjectID("41224d776a326fb40f000009")
}

这是mongo记录的样子

{
    "_id" : ObjectId("5ae3a4d6186af4a878dda65d"),
    "_user" : ObjectId("41224d776a326fb40f000001"),
    "__v" : 0,
    "_cards" : [ 
        [ 
            {
                "cardType" : "MISSION",
                "cardId" : ObjectId("41224d776a326fb40f000007")
            }
        ], 
        [ 
            {
                "cardType" : "MISSION",
                "cardId" : ObjectId("41224d776a326fb40f000009")
            }
        ]
    ]
}

有什么想法吗?

0 个答案:

没有答案