Skip to content

Instantly share code, notes, and snippets.

@Albert-IV
Created August 15, 2013 18:57
Show Gist options
  • Save Albert-IV/6243682 to your computer and use it in GitHub Desktop.
Save Albert-IV/6243682 to your computer and use it in GitHub Desktop.
Example for changing logic depending on mongo results.
CollectionOne.find( { 'search-term-here' : 'value' }, function(e, col1) {
if (e) return cb(e);
CollectionTwo.find( { 'your-search-here' : 'value' }, function(e, col2) {
if (e) return cb(e);
if (!results || !results.length) {
noPreviousResults(col1, cb);
} else {
previousResults(col1, col2, cb);
}
});
});
var noPreviousResults = function(col1, cb) {
var newCollection = {}; // New record
newCollection = new CollectionTwo(newCollection);
newCollection.save(function(e) {
if (e) return cb(e);
col1.property = 'Some Arbitrary Value'
col1.save(cb);
})
};
var previousResults = function(col1, col2, cb) {
col2.someProperty = 'something';
col1.someOtherProperty = 'something-else';
col2.save();
col1.save(cb);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment