简体   繁体   English

如何在猫鼬中定义嵌套模式?

[英]How to define a nested schema in mongoose?

My mongo record is like this: 我的mongo记录是这样的:

{
    "_id":{"$oid":"5550b6de437f572112a29f1a"},
    "cv_count":177732,
    "gender_info": {"male_count": 50, "female_count": 32}
    "stability_info_list":[{"ratio":8.802558610369414e-05,"total_count":34081,"years":0},{"ratio":5.868372406912943e-05,"total_count":34081,"years":1}],
    "zhineng_id":"IT Manager"
}

I write the schema like this: 我这样写模式:

var ZhinengGenderSchema = new Schema({
    male_count: Number,
    female_count: Number
});

var ZhinengStabilitySchema = new Schema({
    ratio: Number,
    total_count: Number,
    years: Number
});

var ZhinengStats = new Schema({
    cv_count: Number,
    gender_info: ZhinengGenderSchema,
    stability_info_list: [ZhinengStabilitySchema],
    zhineng_id: String
})

But I got this excetion: 但是我感到兴奋:

TypeError: Undefined type `undefined` at `gender_info`
Did you try nesting Schemas? You can only nest using refs or arrays.

so mongoose doesn't support nest schemas? 所以mongoose不支持嵌套模式? But my database has already been there, I cannot change, so how can I define my schema? 但是我的数据库已经在那里,我无法更改,那么如何定义模式?

With mongoose you can define nesting (embedded) schemas in Array, like this: 使用猫鼬,您可以在Array中定义嵌套(嵌入式)架构,如下所示:

var mongoose = require('mongoose'),
    Schema = mongoose.Schema;

var bookSchema = new Schema({
    value: { type: String }
});

var authorSchema = new Schema({
    books: [bookSchema]
});

Or by reference 或参考

var mongoose = require('mongoose'),
    Schema = mongoose.Schema,
    ObjectId = mongoose.Schema.Types.ObjectId;

var auhtorSchema = new Schema({
  book: { type: ObjectId, ref: 'Book'}
});

You may choose what is more appropriate for you 您可以选择更适合您的

Just don't create a new schema for the subdocuments and you should be fine, ie: 只是不要为子文档创建新的架构,就可以了,即:

var ZhinengGenderSchema = {
    male_count: Number,
    female_count: Number
};

var ZhinengStabilitySchema = {
    ratio: Number,
    total_count: Number
    years: Number
};

var ZhinengStats = new Schema({
    cv_count: Number,
    gender_info: ZhinengGenderSchema,
    stability_info_list: [ZhinengStabilitySchema],
    zhineng_id: String
})

As far as I know, this is due to a current limitation of Mongoose. 据我所知,这是由于猫鼬的当前限制。 You cannot declare a schema field to include a single sub-document: you have to use an array, instead. 您不能声明架构字段以包含单个子文档:您必须使用数组。 See this: https://github.com/Automattic/mongoose/pull/585 看到这个: https : //github.com/Automattic/mongoose/pull/585

You can set up later the proper business logic in order to ensure that only one sub-element will be added. 您可以稍后设置适当的业务逻辑,以确保仅添加一个子元素。

Try this: 尝试这个:

var ZhinengStats = new Schema({
    cv_count: Number,
    gender_info: [ZhinengGenderSchema],
    stability_info_list: [ZhinengStabilitySchema],
    zhineng_id: String
})

This way, each sub-document has got its own _id in MongoDB (even though it does not lie in a specific collection). 这样,每个子文档在MongoDB中都有自己的_id (即使它不在特定的集合中)。 See more: http://mongoosejs.com/docs/subdocs.html 查看更多: http : //mongoosejs.com/docs/subdocs.html

You could also prefer something like this: 您还可以喜欢这样的东西:

var ZhinengStats = new Schema({
    cv_count: Number,
    gender_info: [{male_count: Number, female_count: Number}],
    stability_info_list: [ZhinengStabilitySchema],
    zhineng_id: String
})

In this case, you nest a schema inside another. 在这种情况下,您将一个架构嵌套在另一个架构中。 The single gender_info element does not have the dignity of a document. 单个gender_info元素不具有文档的尊严

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM