6

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..

1

2 Answers 2

13

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();
Sign up to request clarification or add additional context in comments.

3 Comments

Project is the right way to use. But It's not true that projection options doesn't work. It's just because the code of the author doesn't pass the right arguments for the otions.
query failed: Unsupported projection option: projection: { password: 1.0 } please test and then comment
please check your version of mongodb. projection is only available on version > 3. use fields instead if your mongodb version < 3.
1

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.

1 Comment

for version >3. fields is deprecated. You should you projection instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.