javascript - Mongoose/MongoDB - updating a sub document is not working -
i finding sub document so:
var incidentid = "someid1" var alerteeid = "someid2" incident.findoneq({ _id: someid1, 'alertees._id': someid2 }, {'alertees.$': 1}) .then(function(incident) { var alertee = incident.alertees[0]; alertee.responded_at = date.now() return alertee.parent().saveq().then(function(alertee) { console.log(alertee) }) })
it correctly finds alertee. when update, fails save alertee.
this occurs if position of alertee in array not first. first alertee in array of alertees, able found , updated.
what doing wrong?
your syntax here not "strictly" mongoose syntax, not sure if implementing other layer on top of this.
but want .findoneandupdate()
specified in mongoose documentation. whole update in 1 call, , call this:
incident.findoneandupdate( { "_id": someid1, "alertees._id": someid2 }, { "alertees.$.responded_at": date.now() }, function(err,incident) { if (err) throw err; // or return error if ( incident != null ) { incident.altertees.some(function(alertee) { if ( alertee._id.tostring() == someid2.tostring() ) { console.log( alertee ); return 1; } }); } else { console.log( "not found" ); } } );
that efficient update form return altered document. in response callback i'm using array#some match "alertee" updated , return are.
of course not call methods or hooks defined on "schema", updating date unlikely need it. if can use .findone()
, .save()
have, match array element have before updating instead.
or more complete example:
var async = require('async'), mongoose = require('mongoose'), schema = mongoose.schema, objectid = require('mongodb').objectid; mongoose.connect('mongodb://localhost/test'); var alerteeschema = new schema({ "responded_at": date }); var incidentschema = schema({ alertees: [alerteeschema] }); var incident = mongoose.model( "incident", incidentschema ); var incident = new incident(); var date1 = new date(); var date2 = new date( date1.valueof() - ( date1.valueof() % 1000 * 60 * 60 * 24 ) ); incident.alertees.push({ "responded_at": date1 }); incident.alertees.push({ "responded_at": date2 }); incident.save(function(err,incident) { if (err) throw err; var someid1 = incident._id; var someid2 = incident.alertees[1]._id; //var someid2 = new objectid(); console.log( "before: \n" + incident ); incident.findoneandupdate( { "_id": someid1, "alertees._id": someid2 }, { "alertees.$.responded_at": date.now() }, function(err,incident) { if (err) throw err; if (incident != null) { incident.alertees.some(function(alertee) { console.log( "test: " + alertee ); if ( alertee._id.tostring() == someid2.tostring() ) { console.log( "after: \n" + alertee ); return 1; } }); } else { console.log( "not found" ); } } ); });
with these results:
before: { __v: 0, _id: 53aba6c73b8d05ef77c2703b, alertees: [ { responded_at: thu jun 26 2014 14:51:19 gmt+1000 (est), _id: 53aba6c73b8d05ef77c2703c }, { responded_at: thu jun 26 2014 08:58:31 gmt+1000 (est), _id: 53aba6c73b8d05ef77c2703d } ] } test: { responded_at: thu jun 26 2014 14:51:19 gmt+1000 (est), _id: 53aba6c73b8d05ef77c2703c } test: { responded_at: thu jun 26 2014 14:51:19 gmt+1000 (est), _id: 53aba6c73b8d05ef77c2703d } after: { responded_at: thu jun 26 2014 14:51:19 gmt+1000 (est), _id: 53aba6c73b8d05ef77c2703d }
Comments
Post a Comment