У меня есть коллекция вопросов, и у меня есть модель магазина. Я хочу, чтобы поле вопросов в модели Store представляло собой массив идентификаторов объектов из коллекции вопросов. поэтому я мог бы использовать заполнение позже. когда я буду делать app.get в будущем, он должен показать мне документ с информацией о магазине и всеми вопросами.как заполнить документ mongoose или сохранить ссылки
var Schema = mongoose.Schema;
var storeSchema = Schema({
name : {type : String},
industry : {type : String},
questions :[{type : Schema.Types.ObjectId, ref : "Questions" }]
})
var questionsSchema = Schema({
question : {type :String}
})
var store = mongoose.model("Store", storeSchema);
var questions = mongoose.model("Questions", questionsSchema)
// questions.create({question: "question2"}, function(err,q){
// console.log("create: " , q)
// })
//something like this
questions.find({}, function(err, doc){
store.create({name : "store 1", industry : "industry 1" , questions : /*get the _ids from the question collection*/ {$push : doc.question}})
})
> db.questions.find().pretty()
{
"_id" : ObjectId("574534a289763c004643fa08"),
"question" : "question1",
"__v" : 0
}
{
"_id" : ObjectId("574534acc90f3f2c0c3d529b"),
"question" : "question2",
"__v" : 0
}
>
@jack_blank вы нашли решение для этого? –