所以這裏是一個開始......這是蒙戈外殼
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