user1267116
user1267116

Reputation: 11

MongoDB Query returning false results

I have streamlined this question for simplicity. Value is passed to the function, and if there is no document with {field1: value}, create that document; otherwise, call another function. However, this query will always find a document, or perhaps fires whatever_function() regardless. Why can I not get (!doc) to be true? This database is operative and queries/updates appropriately except for this issue.

    db.doc.find({field1: value}, function(err, doc) {
        if (!doc) {
            db.doc.save({field1: value});
        }
        else {
            whatever_function();            
        }
    });

Upvotes: 1

Views: 375

Answers (1)

SeVeNDuS
SeVeNDuS

Reputation: 298

Find returns a cursor to the selected documents. Then, you should check that doc length is 0.

Upvotes: 2

Related Questions