MongoDB
 sql >> Database >  >> NoSQL >> MongoDB

Aggiornamento del documento nidificato di Mongoose non riuscito?

Non stai utilizzando i nomi dei campi corretti nel tuo B.update chiamata. Dovrebbe essere invece questo:

B.update(
    { 'PDFs._id': pdf_id },           // <== here
    { $set: {
        'PDFs.$.title': 'new title'   // <== and here
    }}, function (err, numAffected) {
        if(err) throw err;
        assert.equal(numAffected,1);
    }
);

Dovresti anche correggere il tuo reset funzione per non richiamare la sua richiamata fino al save completa:

function reset(cb) {
  B.find().remove();
  // create some data with a nested document A
  var newA = new A( { title : "my title" })
  var newB = new B( { PDFs: newA});
  newB.save(cb);  // <== call cb when the document is saved
}