I couldn't actually find a good async function suited for this task, so I hacked something together using async.forever(). That function will keep running until you callback an "error", which is what you want to do.
var uniqueNumber = 1;
var newUnique;
async.forever(function (next) {
var uniqueNum_string = uniqueNumber.toString();
newUnique = data.id + uniqueNum_string;
db.collection('landmarks').findOne({'id':newUnique}, function(err, data){
if (data){
console.log('entry found!');
uniqueNumber++;
next();
}
else {
console.log('entry not found!');
next('unique!'); // This is where the looping is stopped
}
});
},
function () {
saveLandmark(newUnique);
});
Regarding what you're trying to solve, it seems to me you want to insert a new document with a unique id. If that is the case and you're gonna do it often, I would say this is a highly inefficient approach. If you have a thousand documents in the database you would do a thousand completely meaningless requests to the database before you even get close to a unique id.
A better approach would be to get the first document from the collection sorted descending by id (eg the highest id). Then increase that id by one and try to insert until it's not rejected. Because even if you find a unique id, by the time you save the document another insert might have been made from another client or another instance (in case of load-balancing). This may or might not be a problem in your case, I don't know enough about your app, I just thought you should be aware of the drawbacks with your current approach and my answer.