mongoose findbyidandupdate code example

Example 1: findbyidandupdate return old value

Model.findByIdAndUpdate(id, updateObj, {new: true}, function(err, model) {...

Example 2: mongoose findoneandupdate

// note: this uses async/await so it assumes the whole thing 
// is in an async function 

const doc = await CharacterModel.findOneAndUpdate(
  { name: 'Jon Snow' },
  { title: 'King in the North' },
  // If `new` isn't true, `findOneAndUpdate()` will return the
  // document as it was _before_ it was updated.
  { new: true }
);

doc.title; // "King in the North"

Example 3: mongoose model

const schema = new mongoose.Schema({ name: 'string', size: 'string' });
const Tank = mongoose.model('Tank', schema);

Example 4: mongoose modele

mongoose.connect('mongodb://localhost/gettingstarted', {useNewUrlParser: true,useUnifiedTopology: true});