基于另一个文档的子文档

Subdocument based on another document

我想知道是否可以在 Mongoose 中创建一个基于另一个文档的子文档。

通常我会这样做:

'use strict';

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

var CountrySchema = new Schema({
    name: String,
    info: String,
    cities : [
        {
            type: Schema.ObjectId, 
            ref: 'Ressource'
        }
    ]
});

module.exports = mongoose.model('Country', CountrySchema);

然而,这会创建一个新文档,然后通过 id 将其引用到 country 文档。这不是我想要的,我希望 resource 文档嵌套在 country 文档中。我该怎么做?

我发现这解决了我的问题:

'use strict';

var mongoose = require('mongoose'),
    Schema = mongoose.Schema,
    ressourceSchema = require('../ressource/ressource.model');

var CountrySchema = new Schema({
    name: String,
    info: String,
    cities : [
        ressourceSchema.schema
    ]
});

module.exports = mongoose.model('Country', CountrySchema);