简体   繁体   中英

MongoDB object not updating

I am working on a database system using MongoDB. I am trying to save the current date and time, then update it if the same barcode on ID cards is scanned twice, thrice, etc... However, the $set will not update the item. I have already looked at MongoDB documentation and other Stack Overflow posts, but nothing seems to work. Other Stack Overflow posts suggested adding

{ new: true }

and

( overwrite: true }

I tried these both separately and in tandem neither worked.

My Code:

Student.findOne({StudNum: studNum}, function(err, studNumItem) {
   if (err) {
    res.send("MongoDB Error: " + err);
    return false;
   }
   if (!studNumItem) {
    var myData = new Student({ StudNum: studNum, Attendance : 1, LastDateTimeAttended : {
        Year: year, Month: month, Day: day, Hours: hours, Min: min, Sec: sec
    }});
    myData.save()
      .then(item => {
        res.send("saved to database: " + studNum + ", with attendance " + Attendance + "");
      })
      .catch(err => {
        res.send("unable to save to database: " + studNum + ", for attendance " + Attendance + "");
      });
    }
    else{
      var conditions = {StudNum: studNum};
      var update = {$inc : { Attendance: 1 }, $set : {
        "Year": year, "Month": month, "Day": day, "Hours": hours, "Min": min, "Sec": sec
      }};
      Student.findOneAndUpdate(conditions, update, { new: true }, function (err)
      {
          if (err) // If error
          {
            res.send(err);
          }
          else {
            res.send("Checked in!")
          }
      });
 }

});

And my Schema:

var studentSchema = mongoose.Schema({
StudNum: String,
Attendance: Number,
LastDateTimeAttended: {
  Year: Number,
  Month: Number,
  Day: Number,
  Hours: Number,
  Min: Number,
  Sec: Number
}
});

Thanks in advance!

Edit: Just to clarify, saving the item works fine, but updating the item does not, also no errors are thrown while updating.

In your case, i'm not sure it is your find one and update that is the issue. I believe since what you are trying to update is a nested object, you need to prepend it with the top level property to get mongoose to save it. Since those properties don't exist at a top level, mongoose is just throwing it away.

var update = {$inc : { Attendance: 1 }, $set : {
    "LastDateTimeAttended.Year": year, "LastDateTimeAttended.Month": month, "LastDateTimeAttended.Day": day, "LastDateTimeAttended.Hours": hours, "LastDateTimeAttended.Min": min, "LastDateTimeAttended.Sec": sec
  }};

i suggest you try saving you date as a string. LastDateTimeAttended in your model should expect a string, and new Student constructor should create LastDateTimeAttended variable by calling

let date = new Date();
LastDateTimeAttended = date.toString();

your new schema should look like

var studentSchema = mongoose.Schema({
StudNum: {type: String, required: true},
Attendance: {type: Number, required: true},
LastDateTimeAttended: {type: String, required: true}
});

you should then be able to update your mongodb document Student.findOneandUpdate(conditions, update, callback). see working with mongoose Model.findOneandUpdate

对于那些不知道问题原因的人,也可能是方案中未声明变量,导致任何更改被暂停和忽略。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM