Q:

How to get results array data after update on mongodb in node?

Below is my code
var mongodb = require('mongodb');
var MongodbClient = mongodb.MongoClient;

MongodbClient.connect('mongodb://localhost/test', function(err, db) {
    if(!err){
        console.log("We are connected!!");
    }

    var contact = db.collection('contact');

    contact.update({name: "Fred"}, {$set: {tel:'09088oooxxaa'}}, function(err,r) {
       if(err){
           console.log("Update err");
       }
       else{
           console.log('Update success');
           console.log(r.name);
       }
   });

   contact.find({name: "Fred"}).toArray(function(err, results) {
       console.log(results[0]);
   });
});

I can get the result array by using find() method in the end of this code.
However, I would like to know is any way I can get same results array in update function code by callback?
I tried to worte "console.log(r.name) in update code but show undefined

A:

You could also do a findAndModify so you don't need to do the find after the update because it returns the updated item too:
var mongodb = require('mongodb');
var MongodbClient = mongodb.MongoClient;

MongodbClient.connect('mongodb://localhost/test', function(err, db) {
    if(!err){
        console.log("We are connected!!");
    }

    var contact = db.collection('contact');

    contact.findAndModify({name: "Fred"}, {$set: {tel:'09088oooxxaa'}}, function(err,r) {
       // r now contains the entire item updated
       if(err){
           console.log("Update err");
       }
       else{
           console.log('Update success');
           console.log(r.name);
       }
   });
});

