forked from meanjs/mean
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathupgrade-users-sparse-index.js
63 lines (51 loc) · 1.66 KB
/
upgrade-users-sparse-index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
'use strict';
// Set the Node ENV
process.env.NODE_ENV = 'development';
var chalk = require('chalk'),
mongoose = require('../config/lib/mongoose');
mongoose.loadModels();
var _indexToRemove = 'email_1',
errors = [],
processedCount = 0;
mongoose.connect(function (db) {
// get a reference to the User collection
var userCollection = db.connections[0].collections.users;
console.log();
console.log(chalk.yellow('Removing index "' +
_indexToRemove + '" from the User collection.'));
console.log();
// Remove the index
userCollection.dropIndex(_indexToRemove, function (err, result) {
var message = 'Successfully removed the index "' + _indexToRemove + '".';
if (err) {
errors.push(err);
message = 'An error occured while removing the index "' + _indexToRemove + '".';
if (err.message.indexOf('index not found with name') !== -1) {
message = 'Index "' + _indexToRemove + '" could not be found.' +
'\r\nPlease double check the index name in your ' +
'mongodb User collection.';
}
reportAndExit(message);
} else {
reportAndExit(message);
}
});
});
function reportAndExit(message) {
if (errors.length) {
console.log(chalk.red(message));
console.log();
console.log(chalk.yellow('Errors:'));
for (var i = 0; i < errors.length; i++) {
console.log(chalk.red(errors[i]));
if (i === (errors.length -1) ) {
process.exit(0);
}
}
} else {
console.log(chalk.green(message));
console.log(chalk.green('The next time your application starts, ' +
'Mongoose will rebuild the index "' + _indexToRemove + '".'));
process.exit(0);
}
}