roger
roger

Reputation: 9933

How to define a nested schema in mongoose?

My mongo record is like this:

{
    "_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?

Upvotes: 0

Views: 2130

Answers (3)

bFunc
bFunc

Reputation: 1465

With mongoose you can define nesting (embedded) schemas in Array, like this:

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

Upvotes: 1

Emanuele Casadio
Emanuele Casadio

Reputation: 595

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

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

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.

Upvotes: 0

user3434845
user3434845

Reputation: 49

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
})

Upvotes: 1

Related Questions