Home > Enterprise >  MongoDB - How can I get the size of find() using Node.js?
MongoDB - How can I get the size of find() using Node.js?

Time:04-28

I'm writing a server-side code using node.js, I'm trying to get the size of the returned data ( find() ). This is my code

app.post('/login', function(req, res) {
    User.find({
        username: req.body.username,
        password: req.body.password
    }, function(err, users) {
        if (err) {
            res.status(500).send({
                error: "Couldn't find the user"
            })
        } else {
            users.toArray(function(error, n) {
                if (n.length == 0) {
                    console.log("User not found!");
                }

            });
            res.send(users);
        }
    });

});

CodePudding user response:

I'm pretty sure .find() returns an array of objects, so you can just do .length on it

console.log(users.length)
  • Related