为不应生成ID的字段生成的ID

我正在关注MERN stack上的教程,我们创建了一条增加用户体验的途径

当我使用邮递员将任何内容添加到体验字段时,都会生成一个附加的id字段。 如何生成ID?。如果经验是不同的架构,并且我已将其链接到配置文件架构,则可能会生成id,但在这种情况下,我认为没有任何理由。 enter image description here enter image description here

配置文件架构

 const mongoose=require('mongoose')

 const ProfileSchema=new mongoose.Schema({

  user: {
    type: mongoose.Schema.Types.ObjectId,ref: 'user'
},company: {
    type: String
},website: {
    type: String
},location: {
    type: String
},status: {
    type: String,required: true
},skills: {
    type: [String],bio: {
    type: String
},githubusername: {
    type: String
},experience: [
    {
        title: {
            type: String,required: true
        },company: {
            type: String,location: {
            type: String
        },from: {
            type: Date,to: {
            type: Date
        },current: {
            type: Boolean,default: false
        },description: {
            type: String
        }
    }
],education: [
    {
        school: {
            type: String,degree: {
            type: String,fieldofstudy: {
            type: String,social: {
    youtube: {
        type: String
    },twitter: {
        type: String
    },facebook: {
        type: String
    },linkedin: {
        type: String
    },instagram: {
        type: String
    }
},date: {
    type: Date,default: Date.now
}
})


module.exports = Profile = mongoose.model('profile',ProfileSchema);

增加经验的途径

router.put('/experience',[auth,[
check('title',"title is required").not().isEmpty(),check('company',"company is required").not().isEmpty(),check('from',"from date is required").not().isEmpty()
 ] ],async(req,res)=>{
const errors=validationResult(req)
if(!errors.isEmpty()){
    return res.status(400).json({errors:errors.array()})
}
const {
    title,company,location,from,to,current,description
}=req.body

const newExp={
    title,description
}
try{
    const profile=await Profile.findOne({user:req.user.id})
    profile.experience.unshift(newExp)
    await profile.save()
    res.json(profile)

}catch(e){
    console.log(e.message)
    res.status(500).send('server error')
}
 })
lin_504 回答:为不应生成ID的字段生成的ID

_id字段由MongoDb生成,它也自动为其创建索引,并使用该索引对文档进行任何CRUD操作。 您可以检查MongoDb DocumentId field

本文链接:https://www.f2er.com/3087018.html

大家都在问