lundi 2 mars 2015

How to PUT an object in an array in MongoDB using Mongoose on form submission

My mongoDB schema looks like this:



var userSchema = mongoose.Schema({

local : {
email : String,
password : String,
},
userInfo : {
fullname : String,
region : String,
},
milesLog : []

});


And I have the following HTML Form:



<form action="/miles" method="put">
<div class="form-group">
<label>Miles</label>
<input type="text" class="form-control" name="miles">
</div>
<button type="submit" class="btn btn-warning btn-lg">Submit</button>
</form>


And I have following route:



app.put('/miles', isLoggedIn, function(req, res) {

// WHAT TO DO HERE
});


Question


How to PUT an object containing the miles variable into the milesLog array within my user schema on form submission.


Here's an image of my MongoDB:


enter image description here


So after form submission my Database should look like this:



{
"_id": {
"$oid": "54eda3160fb053cc25a9e287"
},
"userInfo": {
"region": "Europe - UK",
"fullname": "max26"
},
"local": {
"password": "$2a$08$xicDozPMtIiImhwUNuV6SO0llxEnHUK3VlzNh6G7OUgbJwfoxTECC",
"email": "max@gmail.com"
},

"milesLog: [
{"miles": "21"},
{"miles": "55"}
],

"__v": 0
}


Thank you for your Help.


Regards,


Aucun commentaire:

Enregistrer un commentaire