Mongoose Schema.update不会更新布尔值

时间:2015-11-30 11:02:12

标签: node.js mongodb express

我已尝试更新其他字段,但效果很好。

我在API中使用的命令:

  User.update({ email: targetUser.email }, { $set: { isAdmin: true }, $push: { 'log.updated': new Date() } }, function (err, user) {
  if (err) {
    responseObject.err = err;
    responseObject.data = null;
    responseObject.code = 422;

    return res.json(responseObject);
  }


  return res.json(responseObject);
});

为了澄清,当我尝试运行它时,API返回代码200,意味着一切正常,但是当我检查数据库时,isAdmin值没有改变。

任何建议都会有所帮助,在这里用完了想法!

请求的用户架构:

var UserSchema = new Schema({
  name: { type: String, default: "", index: 'text' },
  email: { type: String, lowercase: true },
  role: { type: String, default: "" },
  meta: {
    skills: { type: Array, default: [], index: 'text' },
    about: { type: String, default: "", index: 'text' },
    education: { type: Array, default: [], index: 'text' },
    location: {
      address: {
        a: { type: String, default: "" },
        p: { type: String, default: "" },
        c: { type: String, default: "" }
      },
      geo: {
        lat: { type: Number, default: 0 },
        lng: { type: Number, default: 0 }
      }
    }
  },

  compMeta:
  {
    departments: { type: Array, default: [], index: 'text' },
    employees:
    [
      {
        emId: Number,
        empName: String,
        empDep: String  // Dunno if i should use Dep name or Dep ID gonna look in to that later
      }
    ],
  }
  ,
  settings: {
    search: {
      distance: {
        n: { type: Number, default: 100 },
        t: { type: String, default: "km" }
      }
    }
  },
  created: {
    type: Date,
    default: Date.now
  },
  //Rating is an array of objects that consist of rateing 0-100 , job database id , comments from the Company
  rating:
  [
    {
      rate: Number,
      jobId: Number,
      jobComments: String
    }
  ],
  /*rating:
  {
    userTotalRating: {type: Number, default: 0},
    ratingCounter : {type: Number, default: 0}
  }*/


  sensitive: {
    cpr_cvr: String,
  },
  stripe: { type: String },
  facebook: {},
  linkedin: {},
  log: {
    updated: { type: Array, default: [] }
  },
  hashedPassword: String,
  provider: { type: String, default: 'local' },
  salt: String
});

更新

Mongodb版本:3.0.7

3 个答案:

答案 0 :(得分:2)

原来我只是忘了将isAdmin字段添加到我的用户架构中!另外,我对更新的调用是错误的,我将其更改为:

User.update({ email: targetUser.email }, { $set: { isAdmin: true }}, { $push: { 'log.updated': new Date() } }, function (err, user) {
  if (err) {
    responseObject.err = err;
    responseObject.data = null;
    responseObject.code = 422;

    return res.json(responseObject);
  }


  return res.json(responseObject);
});

感谢大家努力帮助我! :)

答案 1 :(得分:0)

尝试使用$ set

更新两个字段
User.update({ email: targetUser.email }, { $set: { isAdmin: true, 'log.updated': new Date() } }, function (err, user) {
 if (err) {
responseObject.err = err;
responseObject.data = null;
responseObject.code = 422;

return res.json(responseObject);
 }
 return res.json(responseObject);
});

希望它有效。

答案 2 :(得分:0)

我遇到了类似的问题。解决方案是添加回调。

这不起作用:

Ride.updateOne({driver:req.body.id},{$set:{isBusy:true}});

这有效:

Ride.updateOne({driver:req.body.id},{$set:{isBusy:true}},(e,s)=>{});