MongoDB Node findone how to handle no results?

mongodb, node.js

Solution

Not finding any records isn't an error condition, so what you want to look for is the lack of a value in `result`. Since any matching documents will always be "truthy", you can simply use a simple `if (result)` check. E.g.,

collection.findOne({query}, function(err, result) {
    if (err) { /* handle err */ }

    if (result) {
        // we have a result
    } else {
        // we don't
    }
}

Problem

Im using the npm `mongodb` driver with node. I have ``` collection.findOne({query}, function(err, result) { //do something } ``` The problem is say I dont have any results, `err` is still `null` whether I find a result or don't. How would I know that there were no results found with the query? I've also tried ``` info = collection.findOne(.... ``` But the `info` is just `undefined` (it looked asynchronous so I didn't think it was the way to go anyway..)

Original source