1
const SurveySchema = new Schema({
name: String,
pages: [ //um survey pode ter várias páginas
{
type: Schema.Types.ObjectId,
ref: 'Page'
}
]
});
const PageSchema = new Schema({
name: String,
description: String,
_type: String
});
const Page = mongoose.model('Page', PageSchema);
const Survey = mongoose.model('Survey', SurveySchema);
Let’s say I already have it inside my bank (Survey
) this populated information:
{
_id: 'survey1',
name: 'Algum nome',
pages: ['58ff555', '123456789'] // Coleção de id's; cada id é uma página
}
How do I make a reference with the id above?
If I do it the way down, um _id
random will be created and there will be no link with the top model.
var pag = new Page({
name: 'joeys',
description: 'descricao 1'
});
pag.save(function(err, model) {
if (err)
res.send(err);
res.json(model);
});
Another question is this: How to generate, at once, a single json with all the information of these two schemes ?
Let me understand, you want to know how to override the
_id
for an existing value and you want to generate a Join between the two schemas?– KhaosDoctor
@Khaosdoctor wanted to generate a Join between the two schemes. After that, I would like to generate a json with all the information that has been added. How to do this?
– Denali