The MongoDB Topology Management API
The MongoDB Topology Management API is an API to allow to programatically spin up a MongoDB instance, Replicaset or Sharded cluster on your local machine.
Setting up a single instance
It's very simple to create a single running MongoDB instance. All examples are using ES6.
var Server = Server;// Create new instancevar server = 'binary' dbpath: './db'; const init = async { // Perform discovery var result = await server; // Purge the directory await server; // Start process await serverstart; // Stop the process await server;} // start the server;
Setting up a replicaset
It's equally easy to create a new Replicaset instance.
var ReplSet = ReplSet; // Create new instancevar topology = 'mongod' // mongod process options options: bind_ip: 'localhost' port: 31000 dbpath: './db-1' // mongod process options options: bind_ip: 'localhost' port: 31001 dbpath: './db-2' // Type of node arbiterOnly: true // mongod process options options: bind_ip: 'localhost' port: 31002 dbpath: './db-3' replSet: 'rs'; const init = async { // Perform discovery var result = await server; // Purge the directory await server; // Start process await serverstart; // Stop the process await server;} // start the replica set;
Each of the node objects can take the following options at the top level.
Field | Description |
---|---|
arbiter | node should become an arbiter. |
builIndexes | should build indexes on the node. |
hidden | node should be hidden. |
builIndexes | should build indexes on the node. |
priority | node should have the following priority. |
tags | tags for the node. |
slaveDelay | the node slaveDelay for replication. |
votes | additional votes for the specific node. |
The object contains the options that are used to start up the actual mongod
instance.
The Replicaset manager has the following methods
Method | Description |
---|---|
Replset.prototype.discover | Return the information from running mongod with --version flag. |
Replset.prototype.start | Start the Replicaset. |
Replset.prototype.primary | Return the current Primary server manager. |
Replset.prototype.shardUrl | Return a add shard url string. |
Replset.prototype.url | Return a connection url. |
Replset.prototype.arbiters | Return a list of arbiter managers. |
Replset.prototype.secondaries | Return a list of secondary managers. |
Replset.prototype.passives | Return a list of secondary passive managers. |
Replset.prototype.waitForPrimary | Wait for a new primary to be elected or for a specific timeout period. |
Replset.prototype.stepDownPrimary | Stepdown the primary. |
Replset.prototype.configuration | Return the replicaset configuration. |
Replset.prototype.reconfigure | Perform a reconfiguration of the replicaset. |
Replset.prototype.serverConfiguration | Get the initial node configuration for specific server manager. |
Replset.prototype.addMember | Add a new member to the set. |
Replset.prototype.removeMember | Remove a member to the set. |
Replset.prototype.maintenance | Put a node into maintenance mode. |
Replset.prototype.stop | Stop the replicaset. |
Replset.prototype.restart | Restart the replicaset. |
Replset.prototype.purge | Purge all the data directories for the replicaset. |
Setting up a sharded system
It's a little bit more complicated to set up a Sharded system but not much more.
var Sharded = Sharded; const init = async { // Create new instance var topology = mongod: 'mongod' mongos: 'mongos' ; // Add one shard await topology
The Sharded manager has the following methods
Method | Description |
---|---|
Replset.prototype.discover | Return the information from running mongod with --version flag. |
Replset.prototype.start | Start the Sharded cluster. |
Replset.prototype.stop | Stop the replicaset. |
Replset.prototype.restart | Restart the replicaset. |
Replset.prototype.purge | Purge all the data directories for the replicaset. |
Replset.prototype.addShard | Add a new shard to the cluster. |
Replset.prototype.addConfigurationServers | Add a set of nodes to be configuration servers. |
ReplSet.prototype.addProxies | Add a set of mongo proxies to the cluster. |
ReplSet.prototype.enableSharding | Enable sharding on a specific db. |
ReplSet.prototype.shardCollection | Shard a collection. |