I currently have a schema which looks as follows:
positionsApplied:[{
position_id:String,
index_position: Number
}],
I also have 3 objects which I need to insert into my database:
How can I insert these into my database through an Ajax call? What Ive tried so far:
Adding the data to an array like this:
["58d6b7e11e793c9a506ffe8f", 0, "58c2871414cd3d209abf4fc1", 1, "58d6b7e11e793c9a506ffe7f", 1]
Which would then be passed through my ajax call like this?(unsure)
$.ajax({
url: "/insertPositionIndex",
type: "POST",
dataType: "json",
data: {
newarray
},
success: function (data) {
console.log(data);
}
})
Inserting into the database is where I'm stuck, how do I break down the array in order to insert it?
app.post('/insertPositionIndex', function(req, res){
var object = req.body.ordered_divs;
console.log(req.body);
User.update(
{ "_id": req.user._id},
{ "$push":
{"positionsApplied":
{
// unsure what to do here?
}
}
}
).exec(function (err, result) {
console.log(result);
res.send({ results: result });
});
});
Any help is greatly appreciated! * Note the position_id field in the schema is the div_id in the array, also the index-position field is the index_pos in the array.
user schema:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var User = new Schema({
id: String,
name: String,
companyname: String,
password: String,
email: String,
username: String,
location: String,
role:String,
teamwork:Number,
initiative:Number,
technical_skills: Number,
communication:Number,
employees: String,
profile_image: String,
biodescription: String,
twitter: String,
facebook: String,
instagram: String,
linkedin: String,
biodescription: String,
positionsApplied:[{
position_id:String,
index_position: Number
}],
experience: [{
title: String,
location: String,
company: String,
start: String,
end:String,
description:String
}],
position: [{
_id:String,
title: String,
location: String,
start: String,
term:Number,
description:String,
date: {type: Date, default: Date.now},
applied:[{
candidate_id: String,
profile_image: String,
location: String,
name: String,
_id:String
}],
}],
education: [{
school: String,
location: String,
degree: String,
start: String,
end:String,
description:String,
_id:String
}],
images: [{
one: String,
two: String,
three: String,
four: String,
five:String,
six:String
}],
});
module.exports = mongoose.model('User', User);