Reputation: 473
I currently have the following mongoose Model set up:
var Customer = mongoose.model('Customer', {
firstname : String,
lastname : String,
phone : String,
street : String,
city : String,
state : String,
zip : String,
fixed : Boolean,
readings: [Number]
});
Its being used in the following way:
app.post('/api/findcustomer/:customer_id/:customer_reading', function(req, res) {
Customer.update({_id: req.params.customer_id},
{$push: {readings: req.params.customer_reading}},
{upsert:true},
function(err, customer){
if(err){
console.log(err);
}else{
console.log("Successfully added");
}
});
Customer.findOne({
_id : req.params.customer_id
}, function(err, customer) {
if (err)
res.send(err);
res.json(customer);
});
});
This is currently working. However What I would really like to do is have readings(in my model) be a array of objects that consist of a Reading and a Date. My best attempt at this included the following changes:
readings: [{reading: Number, date: String}]
and
{$push: {readings: {reading: req.params.customer_reading, date: 'Some Date'}}}
This isn't working for me, the array remains empty even though I get a successfully added log in the console. Im unsure how to fix the problem and my search thus far hasn't yielded any results. Its possible that my phasing is wrong while search, is readings in the case im requesting considered an array of subdocuments, or an array of objects, or is just a subdocument itself?
Any help would be appreciated!
Upvotes: 1
Views: 509
Reputation: 473
Ug... It appears my attempt was correct, however I was attempting to add the readings to a customer who was built using the old schema. When I created a NEW customer with the NEW schema and attempted to add the reading objects to the array it worked!
Upvotes: 1