在 mongodb 上为每个集合使用多个架构 [英] Use more than one schema per collection on mongodb
问题描述
我想在 mongodb 中为每个集合使用多个架构,如何使用它......?
当我尝试运行它时,它给了我这个错误:
错误:
I wanted to use more than one schema per collection in mongodb , how to use it....?
It gives me this error when I try to run it:
Error:
OverwriteModelError:编译后无法覆盖 allUsers
模型.
OverwriteModelError:一旦编译就无法覆盖checkInOut
模型.
OverwriteModelError: Cannot overwrite
allUsers
model once compiled.
OverwriteModelError: Cannot overwritecheckInOut
model once compiled.
这是我的 schema.js
Heres my schema.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema
, ObjectId = Schema.ObjectId;
var checkInInfoSchema= new Schema({
name:String,
loginSerialId:Number
});
var loginUserSchema = new Schema({
sn : { type: Number, unique:true }
,uname: {type:String, unique:true}
,pass:String
});
var registerUserSchema = new Schema({
sn : { type: Number, unique:true }
, name: String //his/her name
,pass:String,
companyKey:{type:String},
uname:{type:String,unique:true}
});
var checkInOutSchema = new Schema({
uname: String
,companyKey:String
,task:String
,inTime:String
,outTime:String
,date:{type:String}
,serialId:{type:Number,unique:true}
,online:Boolean
});
//Different schema for same collection "allUsers"
var allUser=mongoose.model('allUsers',loginUserSchema);
var registerUser=mongoose.model('allUsers',registerUserSchema);
//Different schema for same collection "checkInOut"
var checkInOut=mongoose.model('checkInOut',checkInOutSchema);
var checkInInfo=mongoose.model('checkInOut',checkInInfoSchema);
module.exports={
allUser:allUser,
registerUser:registerUser,
checkInOut:checkInOut,
checkInInfo:checkInInfo
};
推荐答案
在 mongoose 中你可以这样做:
In mongoose you can do something like this:
var users = mongoose.model('User', loginUserSchema, 'users');
var registerUser = mongoose.model('Registered', registerUserSchema, 'users');
这两个模式将保存在用户"集合中.
This two schemas will save on the 'users' collection.
更多信息可以参考文档:http://mongoosejs.com/docs/api.html#index_Mongoose-model 或者您可以查看以下 gist 它可能会有所帮助.
For more information you can refer to the documentation: http://mongoosejs.com/docs/api.html#index_Mongoose-model or you can see the following gist it might help.
这篇关于在 mongodb 上为每个集合使用多个架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!