MongoDB Node.js – Return All Except Some Fields Using findOne()

mongodbnode.jsquery

I am trying to use the following script to query for a document but the returned document still contains the field (password) that I don't want.

const client = await MongoClient.connect(
                      MONGODB_CONNECTION_STRING, 
                      { useUnifiedTopology: true }
                   );
const db = client.db(DB_NAME);    
const collection = db.collection(collectionName);    
const data = await collection
                .find(
                    {
                        _id: someId
                    },
                    {
                        password: 0
                    }
                );

It supposed to work as I have read in the mongodb documentation but I didn't find any documentation on findOne in mongodb for node.js.

Best Answer

I think you need to use project(). See the example here - Projections