Francesco Sasso
Francesco Sasso

Reputation: 87

How to get items without specific params from MongoDB using Mongoose (NodeJS)

I have NodeJS API with Mongoose to connect to MongoDB
To get all items from a collection I do this: const users = db.users.aggregate()

This is a example user:

{
    name: 'Name',
    password: '$2a$10$fe', // the password is encrypted
    email: '[email protected]'
}

How can I got the same items but without the password param?

Upvotes: 0

Views: 32

Answers (1)

Mehdi Fracso
Mehdi Fracso

Reputation: 518

Use the projection action from the aggregation pipeline:

db.users.aggregate([ {
   $project: { password: 0 } 
}])

Also if you're not going to use the aggregation pipeline, use the find method instead:

db.users.find({}, { password: 0 })

Upvotes: 2

Related Questions