How to exclude some fields from a document

I have the following simple shema:

var userSchema = new Schema({ name : String, age: Number, _creator: Schema.ObjectId }); var User = mongoose.model('User',userSchema); 

What I want to do is create a new document and return to the client, but I want to exclude the "creator" field from one:

 app.post('/example.json', function (req, res) { var user = new User({name: 'John', age: 45, _creator: 'some ObjectId'}); user.save(function (err) { if (err) throw err; res.json(200, {user: user}); // how to exclude the _creator field? }); }); 

At the end, I want to send the newly created user without the _creator field:

 { name: 'John', age: 45 } 

Is it possible to make a request for a mongoose without too much searching?

PS : preferable to do it

+45
javascript mongodb mongoose express
Jun 22 2018-12-12T00:
source share
7 answers

Another way to handle this at the schematic level is to override toJSON for the model.

 UserSchema.methods.toJSON = function() { var obj = this.toObject() delete obj.passwordHash return obj } 

I came across this question, looking for a way to exclude the hash password from json that I served for the client, and select: false broke the verifyPassword function because it did not retrieve the value from the database at all.

+62
Jun 23 2018-12-12T00:
source share

Documented method

 UserSchema.set('toJSON', { transform: function(doc, ret, options) { delete ret.password; return ret; } }); 

UPDATE. You might want to use the whitelist:

 UserSchema.set('toJSON', { transform: function(doc, ret, options) { var retJson = { email: ret.email, registered: ret.registered, modified: ret.modified }; return retJson; } }); 
+49
Jun 12 '13 at 10:49 on
source share

Go through your question when I try to find a similar answer with pymongo. It turns out that in the mongo shell, with the call to the find () function, you can pass a second parameter that indicates what the result document looks like. When you pass a dictionary with an attribute value of 0, you exclude this field from the entire document that exits this query.

In your case, for example, the query will look like this:

 db.user.find({an_attr: a_value}, {_creator: 0}); 

This excludes the _creator option for you.

In pymongo, the find () function is almost the same. Not sure how to translate it to mongoose. I think this is the best solution, comparable to manually deleting fields.

Hope this helps.

+16
Nov 18 '12 at 5:39
source share

I would use the lodash .pick () or .omit () utilities

 var _ = require('lodash'); app.post('/example.json', function (req, res) { var user = new User({name: 'John', age: 45, _creator: 'some ObjectId'}); user.save(function (err) { if (err) throw err; // Only get name and age properties var userFiltered = _.pick(user.toObject(), ['name', 'age']); res.json(200, {user: user}); }); }); 

Another example:

 var _ = require('lodash'); app.post('/example.json', function (req, res) { var user = new User({name: 'John', age: 45, _creator: 'some ObjectId'}); user.save(function (err) { if (err) throw err; // Remove _creator property var userFiltered = _.omit(user.toObject(), ['_creator']); res.json(200, {user: user}); }); }); 
+9
Aug 6 '14 at 4:29
source share

You can call toObject() in the document to convert it to a simple JS object that you can freely modify:

 user = user.toObject(); delete user._creator; res.json(200, {user: user}); 
+5
Jun 22 2018-12-12T00:
source share

I use Mongoosemask and am very happy with it.

It supports hiding and displaying properties with different names depending on your needs.

https://github.com/mccormicka/mongoosemask

var maskedModel = mongomask.mask (model, ['name', 'age']); // And you're done.

0
Aug 26 '14 at 19:06
source share

Following the MongoDB documentation, you can exclude fields by passing a second parameter to your query, for example:

 User.find({_id: req.user.id}, {password: 0}) .then(users => { res.status(STATUS_OK).json(users); }) .catch(error => res.status(STATUS_NOT_FOUND).json({error: error})); 

In this case, the password will be excluded from the request.

font: https://docs.mongodb.com/v2.8/tutorial/project-fields-from-query-results/#return-all-but-the-excluded-field

0
Nov 22 '17 at 10:46 on
source share



All Articles