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? But my database has already been there, I cannot change, so how can I define my schema?
所以mongoose不支持嵌套模式?但我的数据库已经存在,我无法更改,那么如何定义我的模式呢?
3 个解决方案
#1
Just don't create a new schema for the subdocuments and you should be fine, i.e.:
只是不要为子文档创建一个新的模式,你应该没问题,即:
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
})
#2
With mongoose you can define nesting (embedded) schemas in Array, like this:
使用mongoose,您可以在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
您可以选择更适合您的方式
#3
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
据我所知,这是由于目前Mongoose的限制。您不能声明架构字段包含单个子文档:您必须使用数组。请参阅: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). See more: http://mongoosejs.com/docs/subdocs.html
这样,每个子文档在MongoDB中都有自己的_id(即使它不在特定的集合中)。查看更多: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元素没有文档的尊严。
#1
Just don't create a new schema for the subdocuments and you should be fine, i.e.:
只是不要为子文档创建一个新的模式,你应该没问题,即:
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
})
#2
With mongoose you can define nesting (embedded) schemas in Array, like this:
使用mongoose,您可以在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
您可以选择更适合您的方式
#3
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
据我所知,这是由于目前Mongoose的限制。您不能声明架构字段包含单个子文档:您必须使用数组。请参阅: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). See more: http://mongoosejs.com/docs/subdocs.html
这样,每个子文档在MongoDB中都有自己的_id(即使它不在特定的集合中)。查看更多: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元素没有文档的尊严。