Skip to content

Instantly share code, notes, and snippets.

@navanathjadhav
Created June 20, 2022 15:03
Show Gist options
  • Save navanathjadhav/1c0783bdef2c24f0357cf732122c0462 to your computer and use it in GitHub Desktop.
Save navanathjadhav/1c0783bdef2c24f0357cf732122c0462 to your computer and use it in GitHub Desktop.
Example of Returning only needed fields
/*
* Use $project if you are using aggregate
*/
db.users.aggregate([
{
$project: {
_id: 1,
firstName: 1,
lastName: 1,
email: 1,
avatar: 1,
},
},
]);
/*
* Use Schema.select if you are using mongoose
*/
UserSchema.find().select("_id firstName lastName email avatar");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment