Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

MongoDB – How can I get the size of find() using Node.js?

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);
        }
    });

});

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

console.log(users.length)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading