乐闻世界logo
搜索文章和话题

How to hided __v property in mongoose?

5 个月前提问
3 个月前修改
浏览次数67

6个答案

1
2
3
4
5
6

在Mongoose中,__v属性是一个版本键,默认情况下会添加到MongoDB文档中,用于表示文档的版本。如果要在查询时从结果中排除这个属性,有多种方法可以做到。

1. 查询时明确排除__v属性

在执行查询时,可以通过在查询的projection参数中设置__v: 0来排除这个字段。

javascript
Model.find({}, { __v: 0 }).then(result => { console.log(result); });

2. 使用Schema的选项来全局隐藏__v

在定义Mongoose Schema时,可以设置versionKeyfalse,这样在该模型的所有实例中都会隐藏__v字段。

javascript
const schema = new mongoose.Schema({ name: String }, { versionKey: false }); const Model = mongoose.model('Model', schema); const doc = new Model({ name: 'Hide __v example' }); console.log(doc); // `__v` will not be part of the document

3. 使用虚拟属性来过滤__v

虚拟属性是Mongoose中一个非常灵活的特性,你可以定义一个虚拟属性来获取除去__v字段后的文档对象。

javascript
schema.virtual('documentWithoutVersionKey').get(function() { const obj = this.toObject(); delete obj.__v; return obj; });

之后,你可以使用这个虚拟属性来获取没有__v属性的文档对象。

4. 使用toJSONtoObject转换选项

在Schema定义中,也可以通过设置toJSONtoObject的transform选项来排除__v

javascript
schema.set('toJSON', { transform: function(doc, ret, options) { delete ret.__v; return ret; } }); schema.set('toObject', { transform: function(doc, ret, options) { delete ret.__v; return ret; } });

这样在每次调用toJSON()toObject()方法时,__v字段都会被自动排除。

实例

例如,假设我有一个用户模型,并且我不希望在任何API响应中返回__v字段,我可以在定义该用户Schema时设置versionKey: false

javascript
const userSchema = new mongoose.Schema({ username: String, email: String }, { versionKey: false }); const User = mongoose.model('User', userSchema);

这样,无论何时创建用户模型的实例,都不会包含__v字段。

总结来说,隐藏__v属性可以根据项目要求,在查询时动态排除,或者在模型定义时通过Schema选项进行全局设置。

2024年6月29日 12:07 回复

You can disable the "__v" attribute in your Schema definitions by setting the versionKey option to false. For example:

shell
var widgetSchema = new Schema({ ... attributes ... }, { versionKey: false });

I don't think you can globally disable them, but can only do it per Schema. You can read more about Schema's options here. You might also find the Schema set method helpful.

2024年6月29日 12:07 回复

To disable '__v' property, which is not recommended, use the versionKey schema option:

shell
var Schema = new Schema({...}, { versionKey: false });

To hide it from all queries, which sometimes can be not what you want too, use the select schema type option:

shell
var Schema = new Schema({ __v: { type: Number, select: false}})
2024年6月29日 12:07 回复

Two ways:

  1. {versionKey: false}

  2. when you query, like model.findById(id).select('-__v')

'-' means exclude the field

2024年6月29日 12:07 回复

define a toObject.transform function, and make sure you always call toObject when getting documents from mongoose.

shell
var SomeSchema = new Schema({ <some schema spec> } , { toObject: { transform: function (doc, ret, game) { delete ret.__v; } } });
2024年6月29日 12:07 回复

Try this it will remove _v from every query response.

shell
// transform for sending as json function omitPrivate(doc, obj) { delete obj.__v; return obj; } // schema options var options = { toJSON: { transform: omitPrivate } }; // schema var Schema = new Schema({...}, options);
2024年6月29日 12:07 回复

你的答案