Node.js MongoDB Find with projection to exclude _id still returns it

后端 未结 3 1056
醉酒成梦
醉酒成梦 2020-12-18 18:03

Trying to follow the examples here to filter by using a projection to exclude _id. The _id still returns:

Code

var MongoClient = require(\'mongodb\')         


        
3条回答
  •  佛祖请我去吃肉
    2020-12-18 18:41

    To limit the fields you have to use fields option( dont know about new updates):

    dbase.collection("customers").find({}, {
        fields: { _id: 0 }
    }).toArray(function(err, result) {
        if (err) throw err;
        console.log(result);
        db.close();
    });
    

    UPDATE:

    For version > 3 you have to use projection option instead:

    dbase.collection("customers").find({}, {
        projection:{ _id: 0 }
    }).toArray(function(err, result) {
        if (err) throw err;
        console.log(result);
        db.close();
    });
    

提交回复
热议问题