Node.js 我是否必须在mongoose子文档中明确定义自定义id?
在mongoose中,我可以定义一个名为groups的子文档,并使用自定义的_id,如下所示: 我所有的ID都是字符串Node.js 我是否必须在mongoose子文档中明确定义自定义id?,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,在mongoose中,我可以定义一个名为groups的子文档,并使用自定义的_id,如下所示: 我所有的ID都是字符串 var UserSchema = new Schema({ _id: { type: String }, firstName: { type: String, trim: true, required: true }, lastName: { type: String, trim: true, required: true }, groups
var UserSchema = new Schema({
_id: { type: String },
firstName: { type: String, trim: true, required: true },
lastName: { type: String, trim: true, required: true },
groups: [ {type: String, ref: 'Group'} ]
});
或者我应该这样显式地对_id编码:
groups: [ { _id: {type: String, ref: 'Group'} } ]
在您的子模式中,您可以告诉mongoose对象不要生成_id,如下所示
var group = mobgoose.Schema({
//fields of group
},{ _id : false });
然后,您可以在控制器中或使用此子模式的任何位置手动定义_id