findOneAndUpdate don't update field name

问题描述 投票:-1回答:1

The following is an example of a situation where the user is not allowed to change the field name.

enter image description here

router.post('/bio/firstname', (req, res) => {

  Habalka.findOneAndUpdate(
    {_id: "d9aa8566-75fe-4108-a72e-1b67e79cf40c"},
    {name: 5}
  )
    .then((data) => {
      res.json({error: false, data: data})
    });

});

model

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const HabalkaSchema = new Schema({
  _id: {
    type: String
  },
  bio: {
    firstname: {},
    lastname: {},
    middlename: {},
    company: {}
  },
  rating: [

  ],
  files: [
    {
      _id: {
        type: String
      },
      destination: {
        type: String
      },
      filename: {
        type: String
      },
      path: {
        type: String
      },
      folder: {
        type: String
      },
      info: {
        size: {
          type: Number
        },
        mimetype: {
          type: String
        },
        encoding: {
          type: String
        },
        originalname: {
          type: String
        },
        fieldname: {
          type: String
        },
      },
      date: {
        type: Date,
        default: Date.now
      },
      bio: {
        type: Object
      },
      userId: String,
      guessId: {},
    }
  ],
  date: {
    type: Date,
    default: Date.now
  }
});
module.exports = Habalka = mongoose.model('habalka', HabalkaSchema);
node.js mongodb express mongoose
1个回答
0
投票

So assuming the route works fine you just have a syntax error in your update, try this:


  Habalka.findOneAndUpdate(
    {_id: "d9aa8566-75fe-4108-a72e-1b67e79cf40c"},
    {$set: {name: 5}}
  )
    .then((data) => {
      res.json({error: false, data: data})
    });


© www.soinside.com 2019 - 2024. All rights reserved.