所以这里是一个开始......这是蒙戈外壳
db.questions.insert({name:"jwo", responses:[{question:"your name?", answer:"yomamma"}, {question:"your name?", answer:"pappa"}]});
此创建的文档JSON结构,像这样:现在
> db.questions.findOne();
{
"_id" : ObjectId("4d877e89b75dc42c4709278d"),
"name" : "jwo",
"responses" : [
{
"question" : "your name?",
"answer" : "yomamma"
},
{
"question" : "your name?",
"answer" : "pappa"
}
]
}
环通回答,并用问题的_id设置他们的question_id,然后将其插入新的回复集合
> for(i=0; i<question.responses.length; ++i){
... question.responses[i].question_id = question._id;
... db.responses.insert(question.responses[i]);
... }
> db.responses.findOne();
{
"_id" : ObjectId("4d878059b75dc42c4709278e"),
"question" : "your name?",
"answer" : "yomamma",
"question_id" : ObjectId("4d877e89b75dc42c4709278d")
}
你会想改变db.questions.findOne来找到它们并循环。如果这确实需要一段时间,您可能需要切换到地图缩小功能。
这工作很好,谢谢,你说得对,它需要一段时间:oD – 2011-03-24 14:53:56