MongoDB

Include or Exclude Fields to Return from a Query

The document can specify the inclusion of fields or the exclusion of fields. The specifications have the following forms.

  • Specify to include a field – 1 or TRUE
  • Specify to suppression/Exclude a field – 0 or FALSE
     db.users.find( { Name: 'Himen Patel' }, { Name: 1, Email: 1 } )  

    In the result set, only the Name and Email fields and, by default, the _id field returns in the matching document (users). To remove the _id field from the results specifying it in exclusion.

     db.users.find( { Name: 'Himen Patel' }, { Name: 1, Email: 1, _id:0 } )  

    Leave a comment