qerigan
qerigan

Reputation: 51

MongoDB query result as loop controller

I need to assign random number to object field then check if there is such object in database. If not save it else assign new random value and check db again and so on. I know that the problem is caused by synchronism and I have no clue how to solve it.

while (controller = 0) {
    var x = Math.floor((Math.random() * 10) + 1);
    Model.findOne({'x': x}, function(err, result) {
        if (err) throw err;
        if (!err && !result) controller = 1;
        else controller = 0;
    });
}

Upvotes: 0

Views: 63

Answers (2)

zaSmilingIdiot
zaSmilingIdiot

Reputation: 99

What about using MongoDB's upserts?

Basically, this performs an update of the document if it exists, otherwise inserts a new one. So effectively, you could update the document's property to itself if it exists, otherwise, insert the value as a new document if it does not already exist.

I cannot comment on performance of upserts, but I would think one database hit would be better than the two required to first check existence and then the second to do an insert.

Upvotes: 0

Yaroslav Admin
Yaroslav Admin

Reputation: 14535

CAUTION: I don't know details of your case, but your approach can significantly reduce performance. Especially with big MAX_VALUE for random number.

To solve you problem with asynchronous callback, you can define a function and call it recursively:

function saveIfNotExists() {
    var x = Math.floor((Math.random() * 10) + 1);
    Model.findOne({'x': x}, function(err, result) {
        if (err) throw err;

        if (!result) {
            // save object here
        } else {
            saveIfNotExists();
        }
    });
}

Upvotes: 1

Related Questions