How to get the id of the json data inserted in the Mongo db collection with node js app on Bluemix
<p>I had one json data,I want to pass that JSON data in to Mongo db collection.
The json data is,</p>

<pre><code>json=
{
"customerdetails" : {
    "organId" : "sample",
    "address" : {
        "addressLine1" : "123213",
        "postcode" : "RG16QX"
    },
    "customerName" : "don",
    "dob" : "sample"
},
"transactiondetails" : {
    "bankDetails" : {
        "accountName" : "john",
        "bankAddress" : "sample"
    },
 "brokerId" : "12345"
  }
  }
</code></pre>

<p>I want only customerdetails details json part to be stored in one collection.Then I want to get the id of the inserted json(_id":"nFNBY4GN6m6jjpNEY")..Then I want to replace this id in the customer details value and insert this json in another collection.</p>

<pre><code>{
"customerdetails" :{ "_id":"nFNBY4GN6m6jjpNEY"},

"transactiondetails" : {
"bankDetails" : {
    "accountName" : "john",
    "bankAddress" : "sample"
},
"brokerId" : "12345"
}
}
</code></pre>

<p>The code which I am using is</p>

<pre><code>var jsondata=JSON.parse(json);
 var mongodb = require('mongodb');
var MongoClient = mongodb.MongoClient;
 mongourl="*******";
 MongoClient.connect(mongourl, function (err, db) {
      console.log("inside mongo client");

   if (err) {
    console.log('Unable to connect to the mongoDB server. Error:', err);
   } else {
var collection = db.collection('customerdetails');
collection.insert(jsondata.customerDetails, {safe:true},function (err, result) {
  if (err) {
      console.log("got error");
   console.log(err);
  } else {
    var collectionid=result._id;
    console.log("*********"+collectionid);
      console.log("collection"+collection);
   console.log('Inserted documents ");
  }
  });
  }
</code></pre>

<p>But I am unable to insert in to collection..
And also if it gets inserted how can i get the _id of the inserted data from the collection and replace that with the key value and pass the remaining JSON in to another collection..
Can someone suggest any ideas.
By the way Iam using nodeapp and mongodb service on bluemix.
Thanks</p>