How to use a MongoDB collection in another file

I have one server file that is server.js.

I have a MongoDB connection inside this file like this:

var Db=require('mongodb').Db;
var BSON=require('mongodb').BSONPure;
var Server=require('mongodb').Server;

client=new Db('database' , new Server('127.0.0.1',27017),{safe:false});

client.open(function(err,pClient)
{
    client.collection('userdetails',function(err,collection)
    {
        Ucollection=collection;
    });
});

I have another file named server2.js. In this file I have to check if a username exists or not from Ucollection (this is collection name).

How can I give the MongoDB connection to server2.js? How can I use this collection in server2.js?

You could do something like this

in server.js:

var Db=require('mongodb').Db;
var BSON=require('mongodb').BSONPure;
var Server=require('mongodb').Server;

global.mongoHelper = {};
global.mongoHelper.db = Db;
global.mongoHelper.bson = BSON;
global.mongoHelper.server = Server;


client=new Db('database' , new Server('127.0.0.1',27017),{safe:false});

client.open(function(err,pClient)
{
    client.collection('userdetails',function(err,collection)
    {
        Ucollection=collection;
    });
});

in server2.js

client=new global.mongoHelper.db('database' , new global.mongoHelper.server('127.0.0.1',27017),{safe:false});

client.open(function(err,pClient)
{
    client.collection('userdetails',function(err,collection)
    {
        Ucollection=collection;
    });
});

I think much cleaner way of doing this is to seprate your database configration into seprate file. Like this

in database-config.js

var Db=require('mongodb').Db;
var BSON=require('mongodb').BSONPure;
var Server=require('mongodb').Server;

client=new Db('database' , new Server('127.0.0.1',27017),{safe:false});

module.exports.connectDatabase = function(callback){
   client.open(function(err,pClient)
   {
       if(err){
         console.log(err);
         process.exit(1);
       }

       module.exports.userCollection = pClient.collection('userdetails');
       callback();
   });
}

in server.js

var  database = require('./database-config')
database.connectDatabase(function() {
   //here you can reuse collection like this
   database.userCollection

}

in server2.js

var  database = require('./database-config')
//here you can reuse collection like this
   database.userCollection

I am assuming that server.js is your main file which actually intiate server so when you run your application it connects to database and load required collections which you can use anywhere in your application like I did this is considered as best practice to re-use collections. Let me know if there is any confusion

Well you are a bit mistaken about the whole concept of modularizing the code. For your task, you should not make a second server.js. You can make another module say, verifyUser and require it in your server.js file. You may require it (may be) after your mongodb connection.

server.js

var Db=require('mongodb').Db;
var BSON=require('mongodb').BSONPure;
var Server=require('mongodb').Server;

client=new Db('database' , new Server('127.0.0.1',27017),{safe:false});

client.open(function(err,pClient)
{

     exports.Ucollection=pClient;
  });
});

server2.js

  var mongodb = require('mongodb');
  var mainApp=require('./server');
  var collectionObj=mainApp.Ucollection;
  var collection = new mongodb.Collection(collectionObj, 'userdetails');

Using this collection.you can query like below

  collection.insert(userInfo,{safe:true},function(err, objects) {
    if(!err){
       console.log('Data inserted successfully.');
     }

  });