Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As of 1.2, the recommended way to perform a connection is in documentation:</p> <p><a href="http://mongodb.github.io/node-mongodb-native/driver-articles/mongoclient.html" rel="noreferrer">http://mongodb.github.io/node-mongodb-native/driver-articles/mongoclient.html</a></p> <p>excerpt:</p> <pre><code>var MongoClient = require('mongodb').MongoClient , Server = require('mongodb').Server; var mongoClient = new MongoClient(new Server('localhost', 27017)); mongoClient.open(function(err, mongoClient) { var db1 = mongoClient.db("mydb"); mongoClient.close(); }); </code></pre> <p>You may find that a connection singleton is useful for the current state of the official node.js driver. Below is some sample code that I use:</p> <p>connection.js module:</p> <pre><code>var MongoClient = require('mongodb').MongoClient; var db_singleton = null; var getConnection= function getConnection(callback) { if (db_singleton) { callback(null,db_singleton); } else { //placeholder: modify this-should come from a configuration source var connURL = "mongodb://localhost:27017/test"; MongoClient.connect(connURL,function(err,db){ if(err) log("Error creating new connection "+err); else { db_singleton=db; log("created new connection"); } callback(err,db_singleton); return; }); } } module.exports = getConnection; </code></pre> <p>Referencing module:</p> <pre><code>var getConnection = require('yourpath/connection.js') function yourfunction() { getConnection(function(err,db) { //your callback code } . . . } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload