Home > Software design >  findoneandupdate mongodb for new field that not exist
findoneandupdate mongodb for new field that not exist

Time:11-27

so i try to udpdate new field that not exist on my mongodb models, i try to update it every time user login , but its not push any new fields on the mongodb, can someone tell me where did i do wrong here, here is mycode:

 const userLog = await User.findOneAndUpdate(
        { NIK: req.body.NIK },
        // { loginDate: { $exists: false } },
        { $push: { loginDate: toJSONLocal(date) } },
        { new: true }
      );


console.log("====================================");
      console.log(userLog);
      console.log("====================================");

here is the response :

_id: new ObjectId("6368e40d1f58fd76efb27957"),
  fullname: 'Ja lie',
  password: '$2b$10$DEWgxY/hth/yYIVGOvZDt..RpxKCKm58NbrrDFQgITLB8.cHrHHvG',
  NIK: 'MT220',
  status: 'active',
  department: 'Logistic',
  position: 'Trainee',
  Group_Shift: 'Non Shift',
  role: 'admin',
  createdAt: 1667818509,
  updatedAt: 1669188767,
  __v: 0

CodePudding user response:

When you create your MongoDB model, you must create a model with the key loginDate

  • Related