I want to make an insert in my MongoDB, but it is getting pretty complicated, and it is a simple operation, so I think I am lost at some point.
I have three collections, that I'll simplify for the sake of th question: Sites, Clients, Blacklist.
When a new Client signs up, she instantly gets a Site. Therefore I need to check from the form, if the Client doesn't exist, then if the Client e-mail hasn't been blacklisted, then if the Site doesn't exist, and finally create the elements Client & Site.
I started to do that by callbacks, but it seems overcomplciated for me to do so:
var input = {...}; //assume here I have all the user input
db.collection('Clients').count({'email', input.email}, {limit: 1}, function (err, count) {
if (count > 0) {
db.collection('Blacklist').count({'email', input.email}, {limit: 1}, function (err, count) {
if (count > 0) {
db.collection('Sites').count({'domain', input.domain}, {limit: 1}, function (err, count) {
if (count > 0) {
// CREATE THE ACCOUNT
} else {
res.send("Site already exists.");
}
db.close();
});
} else {
res.send("Client is blacklisted.");
}
db.close();
});
} else {
res.send("Client already exists.");
}
db.close();
});
Any other way to achive this kind of operations with ease?
Ideal for me would be something like:
var input = {...}; //assume here I have all the user input
if ( db.collection('Clients').count({'email', input.email}, {limit: 1}) == 0 &&
db.collection('Blacklist').count({'email', input.email}, {limit: 1}) == 0 &&
db.collection('Sites').count({'domain', input.domain}, {limit: 1}) == 0 ) {
// INSERT HERE
} else {
res.send("Could not insert");
}