Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

projection not working with find query

Hello I'd like to exclude some fields by query,. Im using nodejs

public async getDoc() {
        return new Promise((resolve, reject) => {
            this.database.collection('users').find({email: "value3"}, {password: 0}).toArray((err, result) => {
                if(err) {
                    reject(err)
                }
                resolve(result);
            });
        })
    }

but in the result set I keep getting password field..

like image 886
filemonczyk Avatar asked Dec 13 '22 15:12

filemonczyk


2 Answers

Projection doesn't work with the new nodejs mongodb driver... Instead you will have to use .project() cursor method here

this.database.collection('users')
  .find({ "email": "value3" })
  .project({ "password": 0 })
  .toArray();
like image 67
Ashh Avatar answered Dec 21 '22 10:12

Ashh


Use fields object as 2nd parameter in find method with all fields in that object which you want to get exluding password.

Do it like this:

find({email: "value3"}, {fields: {all_other_fields...: 1}})

You can also try {fields: {password: 0}} as well but I haven't tried it myself that's why I'm not sure that it'll work.

like image 20
Syed Kashan Ali Avatar answered Dec 21 '22 11:12

Syed Kashan Ali