Hi,
I have the following Schema, it looks a bit lengthy but kindly have a look at education, experience and abroad fields
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const ProfileSchema = new Schema({
user: {
type: Schema.Types.ObjectId,
ref: "users"
},
handle: {
type: String,
required: true,
max: 40
},
company: {
type: String
},
website: {
type: String
},
location: {
type: String
},
status: {
type: String,
required: true
},
skills: {
type: [String],
required: true
},
bio: {
type: String
},
githubusername: {
type: String
},
experience: [
{
title: {
type: String,
required: true
},
company: {
type: String,
required: true
},
location: {
type: String
},
from: {
type: Date,
required: true
},
to: {
type: Date
},
current: {
type: Boolean,
default: false
},
description: {
type: String
}
}
],
education: [
{
school: {
type: String,
required: true
},
degree: {
type: String,
required: true
},
fieldofstudy: {
type: String,
required: true
},
from: {
type: Date,
required: true
},
to: {
type: Date
},
current: {
type: Boolean,
default: false
},
description: {
type: String
}
}
],
social: {
youtube: {
type: String
},
twitter: {
type: String
},
facebook: {
type: String
},
linkedin: {
type: String
},
instagram: {
type: String
}
},
date: {
type: Date,
default: Date.now
},
abroad: [
{
country: {
type: String
}
}
]
});
module.exports = Profile = mongoose.model('profile', ProfileSchema);
Now what is happening that when i am creating education, experience or abroad a new id is being created as well, this is happening for fields which i have taken as array, not in other fields, is there something wrong i am doing or is it the way it is
It looks something like below in mongodb
"education": [
{
"current": false,
"_id": "5b46e147a13e32140c69a453",
"school": "AVS",
"degree": "MCA",
"fieldofstudy": "CS",
"from": "2008-02-04T00:00:00.000Z",
"to": "2010-02-03T00:00:00.000Z",
"description": "Study for future job"
}
],
For eg i use below code to add education, same for experience and abroad etc, kindly guide as to why a new id is being generated in mongodb , thanks
router.post(
'/education',
passport.authenticate('jwt', { session: false }),
(req, res) => {
Profile.findOne({ user: req.user.id }).then(profile => {
const newEdu = {
school: req.body.school,
degree: req.body.degree,
fieldofstudy: req.body.fieldofstudy,
from: req.body.from,
to: req.body.to,
current: req.body.current,
description: req.body.description
};
profile.education.unshift(newEdu);
profile.save().then(profile => res.json(profile));
});
}
);