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

后端 未结 3 1039
醉酒成梦
醉酒成梦 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:44

    Starting in version 3.4, there is now an option for adding .project() outside of find().

    Using ES8 async,await.

    Ex:

    async function connectDB(url) {
    
      try {
        const db = await MongoClient.connect(url);     
        const dbase = db.db("db1"); //here  
        
        const results = await dbase.collection("customers").find().project({_id:0}).toArray();
    
           console.log(result);
           db.close();
      }
      catch(err) {
        throw err;
      }
     }

    Docs here and another example here.

提交回复
热议问题