我是猫鼬的新手, 有这样的猫鼬模式:
var userSchema = new mongoose.Schema({
username : {type:String},
password : {type:String},
firstName : {type:String},
lastName : {type:String},
email : {type:String},
userVerified: {type:String},
userType : {type:String},
userGroup : [
{userRoles:{type:String}}
]
},{collection:'user'});
使用req.body.parameters接收模式属性的数据,这里我想创建一次插入多个用户角色的用户记录。
我所做的是:
var userInfo = {'username':username,'password':password,'firstName':firstName,'lastName':lastName,'email':email,'userVerified':userVerified,'userType':userType,'userGroup':{'userRoles':userGroup}}
user(userInfo).save(function (err,doc){});
插入的值如下所示:
{ "_id" : ObjectId("56ab511428f9f92f2b848845"), "username" : "kk", "password" : "dc468c70fb574ebd07287b38d0d0676d", "firstName" : "kk", "lastName" : "kk", "email" : "kk", "userVerified" : "1", "userType" : "user", "userGroup" : [ { "userRoles" : "[\"PHP\",\"NodeJS\"]", "_id" : ObjectId("56ab511428f9f92f2b848846") } ], "__v" : 0 }
和不需要的斜杠也在数组数据中。 以正确的方式帮助解决这个问题。 提前致谢
答案 0 :(得分:0)
尝试使用子文档设置架构。
如果您想将userGroup属性设置为子文档,则需要遵循this。您可以单独为userGroup定义架构:
var userGroup = new Schema({ {
userRoles: {type: String}
}});
var userSchema = new Schema({
username : {type:String},
password : {type:String},
firstName : {type:String},
lastName : {type:String},
email : {type:String},
userVerified: {type:String},
userType : {type:String},
userGroup : [userGroup]
},{collection:'user'});