Skip to content

Commit 820c7c4

Browse files
committed
rs.initiate()
1 parent 89ea6e8 commit 820c7c4

3 files changed

Lines changed: 245 additions & 37 deletions

File tree

README.md

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,117 @@ mongo.command('db.isMaster()', function (err, data) {
8282

8383
```
8484

85+
* rs.conf()
86+
87+
```js
88+
89+
mongo.command('rs.conf()').then(function (data) {
90+
console.log('data = ', data);
91+
});
92+
93+
94+
//data = {
95+
// command: 'mongo --host localhost --port 27017 --eval "rs.conf();"',
96+
// raw: '["MongoDB shell version: 3.2.0\\nconnecting to: localhost:27017/test\\n{\\n\\t\\"_id\\" : \\"mongodb\\",\\n\\t\\"version\\" : 1,\\n\\t\\"protocolVersion\\" : NumberLong(1),\\n\\t\\"members\\" : [\\n\\t\\t{\\n\\t\\t\\t\\"_id\\" : 0,\\n\\t\\t\\t\\"host\\" : \\"mongodb1.quobject.io:27017\\",\\n\\t\\t\\t\\"arbiterOnly\\" : false,\\n\\t\\t\\t\\"buildIndexes\\" : true,\\n\\t\\t\\t\\"hidden\\" : false,\\n\\t\\t\\t\\"priority\\" : 1,\\n\\t\\t\\t\\"tags\\" : {\\n\\t\\t\\t\\t\\n\\t\\t\\t},\\n\\t\\t\\t\\"slaveDelay\\" : NumberLong(0),\\n\\t\\t\\t\\"votes\\" : 1\\n\\t\\t}\\n\\t],\\n\\t\\"settings\\" : {\\n\\t\\t\\"chainingAllowed\\" : true,\\n\\t\\t\\"heartbeatIntervalMillis\\" : 2000,\\n\\t\\t\\"heartbeatTimeoutSecs\\" : 10,\\n\\t\\t\\"electionTimeoutMillis\\" : 10000,\\n\\t\\t\\"getLastErrorModes\\" : {\\n\\t\\t\\t\\n\\t\\t},\\n\\t\\t\\"getLastErrorDefaults\\" : {\\n\\t\\t\\t\\"w\\" : 1,\\n\\t\\t\\t\\"wtimeout\\" : 0\\n\\t\\t}\\n\\t}\\n}\\n",""]',
97+
// lines:
98+
// ['MongoDB shell version: 3.2.0',
99+
// 'connecting to: localhost:27017/test',
100+
// '{',
101+
// '\t"_id" : "mongodb",',
102+
// '\t"version" : 1,',
103+
// '\t"protocolVersion" : NumberLong(1),',
104+
// '\t"members" : [',
105+
// '\t\t{',
106+
// '\t\t\t"_id" : 0,',
107+
// '\t\t\t"host" : "mongodb1.quobject.io:27017",',
108+
// '\t\t\t"arbiterOnly" : false,',
109+
// '\t\t\t"buildIndexes" : true,',
110+
// '\t\t\t"hidden" : false,',
111+
// '\t\t\t"priority" : 1,',
112+
// '\t\t\t"tags" : {',
113+
// '\t\t\t\t',
114+
// '\t\t\t},',
115+
// '\t\t\t"slaveDelay" : NumberLong(0),',
116+
// '\t\t\t"votes" : 1',
117+
// '\t\t}',
118+
// '\t],',
119+
// '\t"settings" : {',
120+
// '\t\t"chainingAllowed" : true,',
121+
// '\t\t"heartbeatIntervalMillis" : 2000,',
122+
// '\t\t"heartbeatTimeoutSecs" : 10,',
123+
// '\t\t"electionTimeoutMillis" : 10000,',
124+
// '\t\t"getLastErrorModes" : {',
125+
// '\t\t\t',
126+
// '\t\t},',
127+
// '\t\t"getLastErrorDefaults" : {',
128+
// '\t\t\t"w" : 1,',
129+
// '\t\t\t"wtimeout" : 0',
130+
// '\t\t}',
131+
// '\t}',
132+
// '}',
133+
// ''],
134+
// object:
135+
// {
136+
// _id: 'mongodb',
137+
// version: 1,
138+
// protocolVersion: { '$numberLong': '1),' },
139+
// members:
140+
// [{
141+
// _id: 0,
142+
// host: 'mongodb1.quobject.io:27017',
143+
// arbiterOnly: false,
144+
// buildIndexes: true,
145+
// hidden: false,
146+
// priority: 1,
147+
// tags: {},
148+
// slaveDelay: { '$numberLong': '0),' },
149+
// votes: 1
150+
// }],
151+
// settings:
152+
// {
153+
// chainingAllowed: true,
154+
// heartbeatIntervalMillis: 2000,
155+
// heartbeatTimeoutSecs: 10,
156+
// electionTimeoutMillis: 10000,
157+
// getLastErrorModes: {},
158+
// getLastErrorDefaults: { w: 1, wtimeout: 0 }
159+
// }
160+
// }
161+
//}
162+
```
163+
164+
* rs.initiate()
165+
166+
```js
167+
168+
mongo.command('rs.initiate()').then(function (data) {
169+
console.log('data = ', data);
170+
});
171+
172+
173+
//data = {
174+
// command: 'mongo --host localhost --port 27017 --eval "rs.initiate()"',
175+
// raw: '["MongoDB shell version: 3.2.0\\nconnecting to: localhost:27017/test\\n{\\n\\t\\"info2\\" : \\"no configuration specified. Using a default configuration for the set\\",\\n\\t\\"me\\" : \\"mongodb1.quobject.io:27017\\",\\n\\t\\"info\\" : \\"try querying local.system.replset to see current configuration\\",\\n\\t\\"ok\\" : 0,\\n\\t\\"errmsg\\" : \\"already initialized\\",\\n\\t\\"code\\" : 23\\n}\\n",""]',
176+
// lines:
177+
// ['MongoDB shell version: 3.2.0',
178+
// 'connecting to: localhost:27017/test',
179+
// '{',
180+
// '\t"info2" : "no configuration specified. Using a default configuration for the set",',
181+
// '\t"me" : "mongodb1.quobject.io:27017",',
182+
// '\t"info" : "try querying local.system.replset to see current configuration",',
183+
// '\t"ok" : 0,',
184+
// '\t"errmsg" : "already initialized",',
185+
// '\t"code" : 23',
186+
// '}',
187+
// ''],
188+
// object:
189+
// {
190+
// info2: 'no configuration specified. Using a default configuration for the set',
191+
// me: 'mongodb1.quobject.io:27017',
192+
// info: 'try querying local.system.replset to see current configuration',
193+
// ok: 0,
194+
// errmsg: 'already initialized',
195+
// code: 23
196+
// }
197+
//}
198+
```

lib/index.js

Lines changed: 83 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -75,34 +75,24 @@ module.exports = Mongo;
7575
var extractResult = function (result) {
7676

7777
var extracterArray = [
78-
{
79-
re: /db.isMaster()/,
80-
run: function (resultp) {
81-
var obj = JSON.parse(resultp.raw);
82-
var lines = obj[0].split(os.EOL);
83-
resultp.lines = _.clone(lines);
84-
85-
try{
86-
var tempArray = lines.splice(2);
87-
//console.log('tempArray', tempArray);
88-
89-
var jsonString = tempArray.reduce(function (previousValue, currentValue) {
90-
var cleaned = cleanJsonDate(currentValue.trim());
91-
return previousValue + cleaned;
92-
}, '');
93-
//console.log('jsonString', jsonString);
94-
resultp.object = JSON.parse(jsonString);
95-
96-
97-
} catch (e) {
98-
resultp.error = e;
78+
{
79+
re: /db.isMaster()/,
80+
run: function (resultp) {
81+
return objectStartingRow3(resultp);
82+
}
83+
},
84+
{
85+
re: /rs.conf()/,
86+
run: function (resultp) {
87+
return objectStartingRow3(resultp);
88+
}
89+
},
90+
{
91+
re: /rs.initiate()/,
92+
run: function (resultp) {
93+
return objectStartingRow3(resultp);
9994
}
100-
101-
102-
103-
return resultp;
10495
}
105-
}
10696
];
10797

10898

@@ -125,13 +115,39 @@ var extractResult = function (result) {
125115
};
126116

127117

128-
var cleanJsonDate = function (line) {
118+
var objectStartingRow3 = function (resultp) {
119+
var obj = JSON.parse(resultp.raw);
120+
var lines = obj[0].split(os.EOL);
121+
resultp.lines = _.clone(lines);
122+
123+
try {
124+
var tempArray = lines.splice(2);
125+
//console.log('tempArray', tempArray);
126+
127+
var jsonString = tempArray.reduce(function (previousValue, currentValue) {
128+
var cleaned = cleanJson(currentValue.trim());
129+
return previousValue + cleaned;
130+
}, '');
131+
//console.log('jsonString', jsonString);
132+
resultp.object = JSON.parse(jsonString);
133+
134+
} catch (e) {
135+
resultp.error = e;
136+
}
137+
138+
return resultp;
139+
140+
};
141+
142+
143+
var cleanJson = function (line) {
144+
var parts, re, str, m, value;
145+
129146
if (line.indexOf('ISODate') > -1) {
130-
var parts = line.split(':');
147+
parts = line.split(':');
131148

132-
var re = /ISODate\("(.*)"/g;
133-
var str = line;
134-
var m, value;
149+
re = /ISODate\("(.*)"/g;
150+
str = line;
135151

136152
while ((m = re.exec(str)) !== null) {
137153
if (m.index === re.lastIndex) {
@@ -144,7 +160,43 @@ var cleanJsonDate = function (line) {
144160
}
145161

146162
return util.format('%s : { "$date" : "%s" },', parts[0], value);
163+
} else if (line.indexOf('ObjectId') > -1) {
164+
parts = line.split(':');
165+
166+
re = /ObjectId\("(.*)"/g;
167+
str = line;
168+
169+
while ((m = re.exec(str)) !== null) {
170+
if (m.index === re.lastIndex) {
171+
re.lastIndex++;
172+
}
173+
// View your result using the m-variable.
174+
// eg m[0] etc.
175+
//console.log('m', m);
176+
value = m && m[1] ? m[1] : 'error';
177+
}
178+
179+
return util.format('%s : { "$oid" : "%s" },', parts[0], value);
180+
} else if (line.indexOf('NumberLong') > -1) {
181+
parts = line.split(':');
182+
183+
re = /NumberLong\((.*)/g;
184+
str = line;
185+
186+
while ((m = re.exec(str)) !== null) {
187+
if (m.index === re.lastIndex) {
188+
re.lastIndex++;
189+
}
190+
// View your result using the m-variable.
191+
// eg m[0] etc.
192+
//console.log('m', m);
193+
value = m && m[1] ? m[1] : 'error';
194+
}
195+
196+
return util.format('%s : { "$numberLong" : "%s" },', parts[0], value);
197+
147198
} else {
199+
148200
return line;
149201
}
150202

test/index.js

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ var Mongo = require('../lib/index.js');
1919
var path = require('path');
2020
var should = require('chai').should();
2121
var assert = require('chai').assert;
22-
22+
var util = require('util');
2323

2424

2525
describe('Mongo', function () {
@@ -32,7 +32,50 @@ describe('Mongo', function () {
3232
//});
3333

3434

35-
it('command db.isMaster() should pass', function (done) {
35+
//it('command db.isMaster() should pass', function (done) {
36+
// var mongo = new Mongo({
37+
// host: 'localhost',
38+
// port: 27017
39+
// });
40+
41+
42+
// assert.isNotNull(mongo);
43+
// var failed = false;
44+
// var err = null;
45+
// mongo.command('db.isMaster()').then(function (data) {
46+
// console.log('data = ', data);
47+
// assert.isNotNull(data);
48+
// }).finally(function () {
49+
// //console.log('finally ');
50+
// assert.isFalse(failed);
51+
// assert.isNull(err);
52+
// done();
53+
// });
54+
//});
55+
56+
57+
//it('command rs.conf(); should pass', function (done) {
58+
// var mongo = new Mongo({
59+
// host: 'localhost',
60+
// port: 27017
61+
// });
62+
63+
64+
// assert.isNotNull(mongo);
65+
// var failed = false;
66+
// var err = null;
67+
// mongo.command('rs.conf()').then(function (data) {
68+
// console.log('data = ', util.inspect(data, {depth:10}));
69+
// assert.isNotNull(data);
70+
// }).finally(function () {
71+
// //console.log('finally ');
72+
// assert.isFalse(failed);
73+
// assert.isNull(err);
74+
// done();
75+
// });
76+
//});
77+
78+
it('command rs.initiate(); should pass', function (done) {
3679
var mongo = new Mongo({
3780
host: 'localhost',
3881
port: 27017
@@ -42,8 +85,8 @@ describe('Mongo', function () {
4285
assert.isNotNull(mongo);
4386
var failed = false;
4487
var err = null;
45-
mongo.command('db.isMaster()').then(function (data) {
46-
console.log('data = ', data);
88+
mongo.command('rs.initiate()').then(function (data) {
89+
console.log('data = ', util.inspect(data, { depth: 10 }));
4790
assert.isNotNull(data);
4891
}).finally(function () {
4992
//console.log('finally ');
@@ -55,6 +98,5 @@ describe('Mongo', function () {
5598

5699

57100

58-
});
59-
60101

102+
});

0 commit comments

Comments
 (0)