0

In mongoose, can I define a subdocument called groups with a custom _id as follows:

All my ids are Strings.

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'} ]
}); 

or should I have to explicitly code the _id like so:

groups: [ { _id: {type: String, ref: 'Group'} } ]
4

1 回答 1

0

在您的子模式中,您可以告诉 mongoose 对象不要生成 _id,如下所示;

var group = mobgoose.Schema({
     //fields of group
},{ _id : false });

然后你可以在你的控制器或任何你使用这个子模式的地方手动定义 _id。

于 2013-12-16T12:47:35.947 回答