title: How to use connection file of mongo db in other files of node.js application
body: <p>I have a file, <em>connection.js</em> that I want to use in another file. How can I use this file in another file for inserting and updating a database. 
  For example I have another file named <em>item.js</em> in which I want to add the item details into the database.</p>
  
  <pre><code>var mongodb = require('mongodb');
  
  module.exports = function() {
  
      this.getConnection = function(callback) {
  
          var MongoClient = mongodb.MongoClient;
          var url = 'mongodb://localhost:27017/ShoppingCart';
  
          console.log(url);
  
          MongoClient.connect(url, function(err, db) {
  
              if (err) {
                  console.log('Unable to connect to the mongoDB server. Error:', err);
                  return;
              } else {
                  console.log('Connection established to', url);
                  return callback;
              } //else
  
          }); //MongoClient.connect
  
      }; //Connection
  
  }; //constructor
  </code></pre>
  
  <p><strong>item.js</strong></p>
  
  <p>In this file the <code>addItem</code> function takes a JSON object which I want to store in the database. For that I need to connect to the database and then insert the result, but I don't understand how to connect to the database.</p>
  
  <p>I tried this but it's not working:</p>
  
  <pre><code> /**
    * Shopping Cart
    * @author Sunil Hirole
    * @Date 15-july-2015
    * This is an e-commerce application for purchasing items
    */
  
  var prompt = require('prompt');
  var item = require('../models/itemArray.js');
  var Connection = require('../util/con.js');
  
  /** 
    * Item class is a model class
    * It contains addItem,editItem,showItem,deleteItem functions 
   */
  
  function Item(){
      this.id;
      this.name;
      this.price;
  }//Item 
  
  /**
    *addItem Function 
    **/
  
  Item.prototype.addItem = function(result){
  var connection = new Connection();
  var db = connection.getConnection(function(err,db){});
  var Collection = db.collection('ItemsArray');
  Collection.insert([result], function(err, result) {
      if (err) {
          console.log(err);
          return;
          } 
      else {
          return result;
          }
  //Close connection
  db.close();
   });
    return(result);
    }//addItem
  </code></pre>