FANDOM



The simplest way to change data is to retrieve a record, alter it and save it.

var robot_model = require('./../models/robots');

var robot_model.model().findOne({name: 'r2d2'}, 
  function(err, document){
    document.name = 'r2-d2';
    document.save(function(err, result){ ... })
  } // end findOne callback
);

While this is painless and easy to read, when it comes to optimizing your document, it does have some overhead; you are pulling ALL data about the collection even though your concern is only with a single field. The terser solution is update, but that is best done with Mongo-native.

Note that one of the features of Mongoose is transactional integrity, AKA "CouchDB Envy".

That is, if someone changes a document between the period when you get a model and when you save() it, (an ACID violation), the new data will not be saved and an error will be thrown to the callback. (note - this is one reason to always use the callback even if the rest of the code doesn't reqiure waiting for the record to be saved.)

This is another example of how Mongoose is not a blind gateway to Mongo - as Mongo itself doen't have this kind of transactional feature - but adds features in an effort to make Mongo "Better". "Better" of course is subjective; transactional integrity is pretty useful, but in scenarios where you don't care about transactional integrity, you have to do some error trackng and extra management. (Though there might be some way to disable transactions in Mongoose - anyone know anything about this?)

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.