/ mongoose

Mongoose: Exclude fields to return from Query

In case you want to use MongoDB .exclude methods on your model within Mongoose, but you don't want to abandon native Mongoose and its benefits, starting from Mongoose greater than 3.x (current: 3.8), you can use Mongoose's .select method to construct your query and exclude unwanted fields from returning from MongoDB.

In this handy example we use the '-' flag to exclude a certain path (e.g. 'items.apples') or subdocument from our query:

Now the returned data will look like this:

    oranges: 'Navel',
    grapes: 'Vroege van der Laan'

Note that the apples field is missing from the returned JSON data.

When using string syntax, prefixing a path with - will flag that path as excluded. When a path does not have the - prefix, it is included.

Source: MongooseJS Docs: Query-Select

Mongoose: Exclude fields to return from Query
Share this