我想知道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
文档中。我该怎么做?
答案 0 :(得分:0)
我发现这解决了我的问题:
'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);