1 | # The MongoDB Topology Management API
|
2 |
|
3 | 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.
|
4 |
|
5 | ## Setting up a single instance
|
6 |
|
7 | It's very simple to create a single running MongoDB instance. All examples are using **ES6**.
|
8 |
|
9 | ```js
|
10 | var Server = require('mongodb-topology-manager').Server;
|
11 | // Create new instance
|
12 | var server = new Server('binary', {
|
13 | dbpath: f('%s/db', __dirname)
|
14 | });
|
15 |
|
16 | // Perform discovery
|
17 | var result = yield server.discover();
|
18 | // Purge the directory
|
19 | yield server.purge();
|
20 | // Start process
|
21 | yield server.start();
|
22 | // Stop the process
|
23 | yield server.stop();
|
24 | ```
|
25 |
|
26 | ## Setting up a replicaset
|
27 |
|
28 | It's equally easy to create a new Replicaset instance.
|
29 |
|
30 | ```js
|
31 | var ReplSet = require('mongodb-topology-manager').ReplSet;
|
32 |
|
33 | // Create new instance
|
34 | var topology = new ReplSet('mongod', [{
|
35 | // mongod process options
|
36 | options: {
|
37 | bind_ip: 'localhost', port: 31000, dbpath: f('%s/../db/31000', __dirname)
|
38 | }
|
39 | }, {
|
40 | // mongod process options
|
41 | options: {
|
42 | bind_ip: 'localhost', port: 31001, dbpath: f('%s/../db/31001', __dirname)
|
43 | }
|
44 | }, {
|
45 | // Type of node
|
46 | arbiterOnly: true,
|
47 | // mongod process options
|
48 | options: {
|
49 | bind_ip: 'localhost', port: 31002, dbpath: f('%s/../db/31002', __dirname)
|
50 | }
|
51 | }], {
|
52 | replSet: 'rs'
|
53 | });
|
54 |
|
55 | // Perform discovery
|
56 | var result = yield server.discover();
|
57 | // Purge the directory
|
58 | yield server.purge();
|
59 | // Start process
|
60 | yield server.start();
|
61 | // Stop the process
|
62 | yield server.stop();
|
63 | ```
|
64 |
|
65 | Each of the node objects can take the following options at the top level.
|
66 |
|
67 | Field | Description
|
68 | ----------------------------------|-------------------------
|
69 | arbiter | node should become an arbiter.
|
70 | builIndexes | should build indexes on the node.
|
71 | hidden | node should be hidden.
|
72 | builIndexes | should build indexes on the node.
|
73 | priority | node should have the following priority.
|
74 | tags | tags for the node.
|
75 | slaveDelay | the node slaveDelay for replication.
|
76 | votes | additional votes for the specific node.
|
77 |
|
78 | The **object** contains the options that are used to start up the actual `mongod` instance.
|
79 |
|
80 | The Replicaset manager has the following methods
|
81 |
|
82 | Method | Description
|
83 | ----------------------------------|-------------------------
|
84 | Replset.prototype.discover | Return the information from running mongod with --version flag.
|
85 | Replset.prototype.start | Start the Replicaset.
|
86 | Replset.prototype.primary | Return the current Primary server manager.
|
87 | Replset.prototype.shardUrl | Return a add shard url string.
|
88 | Replset.prototype.url | Return a connection url.
|
89 | Replset.prototype.arbiters | Return a list of arbiter managers.
|
90 | Replset.prototype.secondaries | Return a list of secondary managers.
|
91 | Replset.prototype.passives | Return a list of secondary passive managers.
|
92 | Replset.prototype.waitForPrimary | Wait for a new primary to be elected or for a specific timeout period.
|
93 | Replset.prototype.stepDownPrimary | Stepdown the primary.
|
94 | Replset.prototype.configuration | Return the replicaset configuration.
|
95 | Replset.prototype.reconfigure | Perform a reconfiguration of the replicaset.
|
96 | Replset.prototype.serverConfiguration | Get the initial node configuration for specific server manager.
|
97 | Replset.prototype.addMember | Add a new member to the set.
|
98 | Replset.prototype.removeMember | Remove a member to the set.
|
99 | Replset.prototype.maintenance | Put a node into maintenance mode.
|
100 | Replset.prototype.stop | Stop the replicaset.
|
101 | Replset.prototype.restart | Restart the replicaset.
|
102 | Replset.prototype.purge | Purge all the data directories for the replicaset.
|
103 |
|
104 | ## Setting up a sharded system
|
105 |
|
106 | It's a little bit more complicated to set up a Sharded system but not much more.
|
107 |
|
108 | ```js
|
109 | var Sharded = require('mongodb-topology-manager').Sharded;
|
110 |
|
111 | // Create new instance
|
112 | var topology = new Sharded({
|
113 | mongod: 'mongod', mongos: 'mongos'
|
114 | });
|
115 |
|
116 | // Add one shard
|
117 | yield topology.addShard([{
|
118 | options: {
|
119 | bind_ip: 'localhost', port: 31000, dbpath: f('%s/../db/31000', __dirname)
|
120 | }
|
121 | }, {
|
122 | options: {
|
123 | bind_ip: 'localhost', port: 31001, dbpath: f('%s/../db/31001', __dirname)
|
124 | }
|
125 | }, {
|
126 | // Type of node
|
127 | arbiter: true,
|
128 | // mongod process options
|
129 | options: {
|
130 | bind_ip: 'localhost', port: 31002, dbpath: f('%s/../db/31002', __dirname)
|
131 | }
|
132 | }], {
|
133 | replSet: 'rs1'
|
134 | });
|
135 |
|
136 | // Add one shard
|
137 | yield topology.addShard([{
|
138 | options: {
|
139 | bind_ip: 'localhost', port: 31010, dbpath: f('%s/../db/31010', __dirname)
|
140 | }
|
141 | }, {
|
142 | options: {
|
143 | bind_ip: 'localhost', port: 31011, dbpath: f('%s/../db/31011', __dirname)
|
144 | }
|
145 | }, {
|
146 | // Type of node
|
147 | arbiter: true,
|
148 | // mongod process options
|
149 | options: {
|
150 | bind_ip: 'localhost', port: 31012, dbpath: f('%s/../db/31012', __dirname)
|
151 | }
|
152 | }], {
|
153 | replSet: 'rs2'
|
154 | });
|
155 |
|
156 | // Add configuration servers
|
157 | yield topology.addConfigurationServers([{
|
158 | options: {
|
159 | bind_ip: 'localhost', port: 35000, dbpath: f('%s/../db/35000', __dirname)
|
160 | }
|
161 | }, {
|
162 | options: {
|
163 | bind_ip: 'localhost', port: 35001, dbpath: f('%s/../db/35001', __dirname)
|
164 | }
|
165 | }, {
|
166 | options: {
|
167 | bind_ip: 'localhost', port: 35002, dbpath: f('%s/../db/35002', __dirname)
|
168 | }
|
169 | }], {
|
170 | replSet: 'rs3'
|
171 | });
|
172 |
|
173 | // Add proxies
|
174 | yield topology.addProxies([{
|
175 | bind_ip: 'localhost', port: 51000, configdb: 'localhost:35000,localhost:35001,localhost:35002'
|
176 | }, {
|
177 | bind_ip: 'localhost', port: 51001, configdb: 'localhost:35000,localhost:35001,localhost:35002'
|
178 | }], {
|
179 | binary: 'mongos'
|
180 | });
|
181 |
|
182 | // Start up topology
|
183 | yield topology.start();
|
184 |
|
185 | // Shard db
|
186 | yield topology.enableSharding('test');
|
187 |
|
188 | // Shard a collection
|
189 | yield topology.shardCollection('test', 'testcollection', {_id: 1});
|
190 |
|
191 | // Stop the topology
|
192 | yield topology.stop();
|
193 | ```
|
194 |
|
195 | The Sharded manager has the following methods
|
196 |
|
197 | Method | Description
|
198 | ----------------------------------|-------------------------
|
199 | Replset.prototype.discover | Return the information from running mongod with --version flag.
|
200 | Replset.prototype.start | Start the Sharded cluster.
|
201 | Replset.prototype.stop | Stop the replicaset.
|
202 | Replset.prototype.restart | Restart the replicaset.
|
203 | Replset.prototype.purge | Purge all the data directories for the replicaset.
|
204 | Replset.prototype.addShard | Add a new shard to the cluster.
|
205 | Replset.prototype.addConfigurationServers | Add a set of nodes to be configuration servers.
|
206 | ReplSet.prototype.addProxies | Add a set of mongo proxies to the cluster.
|
207 | ReplSet.prototype.enableSharding | Enable sharding on a specific db.
|
208 | ReplSet.prototype.shardCollection | Shard a collection.
|