Skip to content

Instantly share code, notes, and snippets.

@CiscoKidxx
Created August 20, 2015 16:51
Show Gist options
  • Save CiscoKidxx/4d7503dc3e33245c6fce to your computer and use it in GitHub Desktop.
Save CiscoKidxx/4d7503dc3e33245c6fce to your computer and use it in GitHub Desktop.
function mongoSqlCompare () {
// set empty arrays
var mongo_users = []
var sql_users = []
// find all user objects in mongo db
stUser.find({}, function(err, res) {
if (err) throw err;
// jsonify the mongo db collections
var jsonString = JSON.stringify(res);
var jsonData = JSON.parse(jsonString);
// loop through each document and push GuiLoginName[i] to array as a string
jsonData.forEach(function(element, i) {
mongo_users.push(jsonData[i].GuiLoginName);
})
// query sql database for all users
connection.query('SELECT * from users WHERE LDAPUser = 1', function(err, res) {
if (err) throw err;
// jsonify the response
var json_string = JSON.stringify(res);
var sqlUsers = JSON.parse(json_string);
// loop through each sql user object
sqlUsers.forEach(function(element, i) {
sql_users.push(sqlUsers[i].GuiLoginName);
});
// For each mongo user
mongo_users.forEach(function(element, i) {
// search for user in Sql users
var isMatch = _.indexOf(sql_users, mongo_users[i])
// Return positive number if the mongo user is in the sql database
if (isMatch >= 0) {
// do nothing
}
// remove the document from mongodb if it does not exist in sql
else {
stUser.find({GuiLoginName: element}).remove(function (err, res) {
if (err) throw err;
console.log('[+] Removed ' + '"' + element + '"' + ' from mongodb. User no longer exists in MySql.')
})
}
});
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment