如题,我在用mongoose的时候,用了两个schema,如下: var Comments = new Schema({ projectName : String , comment : String , created_at : Date });
//定义user对象模型 var UserScheme = new Schema({ userId:ObjectId, username:String, password:String, userAccountId:ObjectId, userAccountSummary:{type:String,default:"1000"}, myProjectsList:[], myCommentsList:[Comments], myFundedProjectsList:[]
}); 但是后来我在我的网站上注册登录后,数据库插进来一个奇怪的字段:__v:0 我插入一条数据后:显示如下结果:
我并没有插入__v从始至终,不知道大家遇到没
哎,多看api啊,_id是默认配置上去的,即便你不写,也会有,如果你重新指定id,那么原有的_id也就没有了,会显示新的主键id。至于这个__v,你回想你写过哪些程序会有v这个符号,例如“nodejs -v”或者“npm -v”或者“java -version”,对了,v代表的就是版本,__v也是默认配置上去的,用来表示版本信息的,当然这个你也可以去掉,具体的查看api去。多看api,下次要是看见多了几个文档例如“system.indexes”或者“system.users”等千万不要惊讶。
@zstar 直接把源发给你看吧
The versionKey is a property set on each document when first created by Mongoose. This keys value contains the internal revision of the document. The name of this document property is configurable. The default is __v. If this conflicts with your application you can configure as such:
var schema = new Schema({ name: 'string' });
var Thing = mongoose.model('Thing', schema);
var thing = new Thing({ name: 'mongoose v3' });
thing.save(); // { __v: 0, name: 'mongoose v3' }
// customized versionKey
new Schema({..}, { versionKey: '_somethingElse' })
var Thing = mongoose.model('Thing', schema);
var thing = new Thing({ name: 'mongoose v3' });
thing.save(); // { _somethingElse: 0, name: 'mongoose v3' }
Document versioning can also be disabled by setting the versionKey to false. DO NOT disable versioning unless you know what you are doing.
new Schema({..}, { versionKey: false });
var Thing = mongoose.model('Thing', schema);
var thing = new Thing({ name: 'no versioning please' });
thing.save(); // { name: 'no versioning please' }
请仔细阅读