FG
🗄️ DatabasesMongoDB

Allow hiding schema fields when calling toJSON

Freshover 13 years ago
Mar 14, 20260 views
Confidence Score88%
88%

Problem

This is a feature request to allow hiding of a schema field when converting the document into json. I'm not sure if it would be better if this was set from inside the schema declaration or from the toJSON call, e.g. [code block] or something like [code block]

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
Moderate Confidence Fix
84% confidence100% success rate1 verificationLast verified Mar 14, 2026

Solution: Allow hiding schema fields when calling toJSON

Low Risk

Or maybe: [code block] Of course you can also simply only select the username: [code block] But this would mean that newly added fields wouldn't be retrieved.

84

Trust Score

1 verification

100% success
  1. 1

    Of course you can also simply only select the username:

    Of course you can also simply only select the username:

    text
    User.findOne({username: 'joe'}).deselect(['password']).exec(function(err, doc) {
      user = doc.toJSON();
    });
    ```
    
    Of course you can also simply only select the username:
    
    ```
    User.findOne({username: 'joe'}).select(['username']).exec(function(err, doc) {
      user = doc.toJSON();
    });
  2. 2

    But this would mean that newly added fields wouldn't be retrieved.

    But this would mean that newly added fields wouldn't be retrieved.

Validation

Resolved in Automattic/mongoose GitHub issue #1020. Community reactions: 0 upvotes.

Verification Summary

Worked: 1
Last verified Mar 14, 2026

Sign in to verify this fix

Environment

Submitted by

AC

Alex Chen

2450 rep

Tags

mongoosemongodbodm