Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (50.2k points)

I want to select only a particular field with the help of the following code:

exports.someValue = function(req, res, next) {

    //query with mongoose

    var query = dbSchemas.SomeValue.find({}).select('name');

    query.exec(function (err, someValue) {

        if (err) return next(err);

        res.send(someValue);

    });

};

But in my JSON, I am receiving the _id also, my document schema only has two fields, _id and name

[{"_id":70672,"name":"SOME VALUE 1"},{"_id":71327,"name":"SOME VALUE 2"}]

Can anybody explain me why I am getting this error?

1 Answer

0 votes
by (108k points)

In MongoDB, the _id field of the document will always be present unless you explicitly eliminate it. For eliminating the _id, refer to the following syntax:

exports.someValue = function(req, res, next) {

    //query with mongoose

    var query = dbSchemas.SomeValue.find({}).select('name -_id');

    query.exec(function (err, someValue) {

        if (err) return next(err);

        res.send(someValue);

    });

};

 

Related questions

0 votes
1 answer
asked Mar 14, 2020 in Web Technology by ashely (50.2k points)
0 votes
2 answers
0 votes
2 answers

Browse Categories

...