猫鼬的一系列文档更新

时间:2019-03-05 20:23:31

标签: mongoose mongoose-schema

我有以下文档:

const questionSchema = new Schema({
  text: { type: String, required: true, unique: true, minLength: 3 },
  approved:{type: String, default: false},
  ups: [{type: Schema.Types.ObjectId, ref:'User'}],
  downs: [{type: Schema.Types.ObjectId, ref:'User'}],
  tags: [{type: Schema.Types.ObjectId, ref: 'Tag'}],
  answers: [{type: Schema.Types.ObjectId, ref: 'Answer'}],
  author: {type: Schema.Types.ObjectId, ref: 'User'}
})

const Question = model('Question', questionSchema);
module.exports = Question;

当我尝试获取它时,请编辑“ ups”和“ downs”的数组,然后将其保存:

Question.findById(questionId)
      .then(q => {
        q.downs.remove(userId);
        q.ups.push(userId);

        q.save()
          .catch(e => {
            console.log(e);
          })
      })

我同时遇到以下错误:

  

在路径“ downs”处,对值[[0]”的[ObjectId]的发布失败

     

在路径“ ups”处,对值“ [0]”的[ObjectId]的发布失败

我也尝试过使用此代码,但是它引发了我无法推送非数组字段的错误:

Question.findByIdAndUpdate(questionId, {$push: {downs: userId}});

我在做什么错了?

0 个答案:

没有答案
相关问题