Mongoose,findByIdAndUpdate,更新所有除了一个?

时间:2018-05-26 12:40:16

标签: express mongoose

我无法理解,但是在创建用户时,日期表示创建帐户的日期。但是当用户更新他们的信息时,“日期”也会更新。我究竟做错了什么?

我的模特:

var userinfoSchema = new mongoose.Schema({
    type_        : String,
    firstname    : String,
    surname      : String,
    organization : String,
    name         : String,
    person_role  : String,
    members_num  : Number,
    address      : String,
    postal_code  : String,
    city         : String,
    country      : String, 
    tel          : String,
    date: { type: Date, default: Date.now }
});

var UserSchema = new mongoose.Schema({
    username: String,
    password: String,
    userinfo: [userinfoSchema]
});

我的应用功能:

app.put("/Edit/:id", function(req,res) {
    User.findByIdAndUpdate(req.params.id, {$set: { userinfo: req.body.userinfo}}, 
    function(err, updated) {
        if(err) {
            res.redirect('/Edit');
        } else {
            console.log(updated);
            res.redirect('/Profil');
        }
    });
});  

2 个答案:

答案 0 :(得分:0)

有两种方法可以使date保持不变。第一种也是最简单的方法是清理来自客户端的数据:

app.put("/Edit/:id", (req,res) => {
  delete req.body.userinfo.date;

  User.findByIdAndUpdate(req.params.id, {$set: { userinfo: req.body.userinfo}}, (err, updated) => {
    // ...
  });
});

您可能应该对数据进行清理和验证,然后再将其直接保存到数据库中。

或者,您可以先查询需要更新的文档,然后使用save()。您的架构如下所示:

var userinfoSchema = new mongoose.Schema({
  type_        : String,
  firstname    : String,
  surname      : String,
  organization : String,
  name         : String,
  person_role  : String,
  members_num  : Number,
  address      : String,
  postal_code  : String,
  city         : String,
  country      : String, 
  tel          : String,
  date         : {
    type         : Date,
    default      : Date.now,
    set          : (date) => {
      if(this.date) {
        return;
      }

      this.date = date;
    }
  }
});

但是,要更新用户,您需要按照以下步骤进行操作:

const _ = require('lodash');

app.put("/Edit/:id", (req,res) => {
  delete req.body.userinfo.date;

  User.findById(req.params.id, (err, user) => {
    if(err) {
      // Handle error.
    }

    _.assign(user, req.body.userinfo);

    user.save().then(/*...*/);
  });
});

答案 1 :(得分:0)

搜索一周后,这是我认为的最佳答案...

从Mongoose 4.0开始,您现在可以在Schema上设置 timestamps 选项,以使Mongoose为我们处理此问题:

var thingSchema = new Schema({..}, { timestamps: true });

所以在这种情况下:模型应该像这样:

var userinfoSchema = new mongoose.Schema({
    type_        : String,
    firstname    : String,
    surname      : String,
    organization : String,
    name         : String,
    person_role  : String,
    members_num  : Number,
    address      : String,
    postal_code  : String,
    city         : String,
    country      : String, 
    tel          : String,
});

var UserSchema = new mongoose.Schema({
    username: String,
    password: String,
    userinfo: [userinfoSchema]
}, { timestamps: true });

就像,我们可能会看到:date:{类型:Date,默认值:Date.now}不是一个好的选择。

时间戳创建两个字段:“ createdAt”和“ updatedAt”,如下图所示:createdAt will never change and updatedAt will change evertytime the user will update their datas

NB:同样,我们可以按“日期”或类似方式更改“ createdAt”:

var UserSchema = new mongoose.Schema({
    username: String,
    password: String,
    userinfo: [userinfoSchema]
}, { createdAt: 'date', updatedAt: 'update_date' });