Create a new MongoClient instance.
- class MongoClient()¶
Arguments:
- serverConfig (object) – server config object.
- [options] (object) – additional options for the collection.
Connect to MongoDB using a url as documented at
docs.mongodb.org/manual/reference/connection-string/
Arguments: |
|
---|---|
Returns: | null |
Initialize the database connection.
Arguments: |
|
---|---|
Returns: | null |
Examples
A basic example using the MongoClient to connect using a Server instance, similar to existing Db version
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); // Set up the connection to the local db var mongoclient = new MongoClient(new Server("localhost", 27017), {native_parser: true}); // Open the connection to the server mongoclient.open(function(err, mongoclient) { // Get the first db and do an update document on it var db = mongoclient.db("integration_tests"); db.collection('mongoclient_test').update({a:1}, {b:1}, {upsert:true}, function(err, result) { assert.equal(null, err); assert.equal(1, result); // Get another db and do an update document on it var db2 = mongoclient.db("integration_tests2"); db2.collection('mongoclient_test').update({a:1}, {b:1}, {upsert:true}, function(err, result) { assert.equal(null, err); assert.equal(1, result); // Close the connection mongoclient.close(); }); }); });
Close the current db connection, including all the child db instances. Emits close event and calls optional callback.
Arguments: |
|
---|---|
Returns: | null |
Create a new Db instance sharing the current socket connections.
Arguments: |
|
---|---|
Returns: | db a db instance using the new database. |
Connect to MongoDB using a url as documented at
docs.mongodb.org/manual/reference/connection-string/
Arguments: |
|
---|---|
Returns: | null |
Examples
Example of a simple url connection string for a single server connection
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); // Connect using the connection string MongoClient.connect("mongodb://localhost:27017/integration_tests", {native_parser:true}, function(err, db) { assert.equal(null, err); db.collection('mongoclient_test').update({a:1}, {b:1}, {upsert:true}, function(err, result) { assert.equal(null, err); assert.equal(1, result); db.close(); }); });Example of a simple url connection string to a replicaset, with acknowledgement of writes.
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); MongoClient.connect("mongodb://localhost:30000,localhost:30001,localhost:30002/integration_test_?w=1", function(err, db) { assert.equal(null, err); assert.ok(db != null); db.collection("replicaset_mongo_client_collection").update({a:1}, {b:1}, {upsert:true}, function(err, result) { assert.equal(null, err); assert.equal(1, result); db.close(); }); });Example of a simple url connection string to a shard, with acknowledgement of writes.
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); MongoClient.connect('mongodb://localhost:50000,localhost:50001/sharded_test_db?w=1', function(err, db) { assert.equal(null, err); assert.ok(db != null); db.collection("replicaset_mongo_client_collection").update({a:1}, {b:1}, {upsert:true}, function(err, result) { assert.equal(null, err); assert.equal(1, result); process.exit(0) db.close(); }); });