node.js mongodb update /db.open() issue

I'm using the node.js mongodo native library.

Here's the code snippet that's not working. The db.open call fails with no error. The console.log statements inside the db.open call are not printed and the db values are also not updated. Frustrating error. No clue as to how to resolve this. Any help will be greatly appreciated.

function updateFriend(friendId, loc) {
        var input = JSON.parse(JSON.stringify("{FRIENDS.PHONE_NUMBER :" + friendId + "}"));
        db.open(function(error, client) {
          console.log("##########################" );
          console.log("######INPUT :::: " + JSON.stringify(input));
          if(error) throw error;
          var collection = new mongodb.Collection(client, 'test_collection');
          collection.update(input, {"$set":{"FRIENDS.$.LOC":"12.32, 88.33"}}, {safe:true}, function(err) {
            db.close();
          });
        });
}

Here's the mongo collection that I'm trying to update:

{ "FRIENDS" : [ { "PHONE_NUMBER" : "1476516777", "LOC" : "", "NAME" : "A2" } ], "OCCASION" : "cafe", "MYLOCATION" : "13.0390433,77.554942", "MYID" : "695913809", "_id" : ObjectId("503b4e41f1d9b7913d000001") }

EDIT: A manual db update with this command works just fine.

db.test_collection.update({"FRIENDS.PHONE_NUMBER":"1599181298"},{$set:{"FRIENDS.$.LOC":"13,14"}},false,true);

Two things

  1. Don't open a new db connection for each request, open it once in your app and reuse the same db instance across the application. If you need access to other db's do the following

    var db1 = db.db('someotherdb')
    
  2. Don't create collection objects, use the db.collection method. The collection object is not mean to be created and it would be a private constructor if it was possible. Do the following.

    var collection = db.collection('mycollection')
    

Documentation for the driver is here

http://mongodb.github.com/node-mongodb-native/

Best of luck