支持oplog的数据模型架构

时间:2014-07-01 07:58:36

标签: javascript mongodb meteor

我正在尝试使用Meteor实施调查应用。我对数据建模的第一个想法是:

我将收集答案的提交集合:

{
  formId: "........",
  values: [
    {question: "What is your name?", answer:"Aykut Yaman"},
    {question: "How old..?", answer:"7"},
    {question: "Hobbies?", answer: ["Kitap Okumak", "Spor Yapmak"]},
    {question: "Foo", answer: "Lorem ipsum ..."}
  ]
}

然后我会查询这样的集合,为了找出调查,如何给出一些问题的特定答案:

submissions.find({$and: [{"values.answer": "7"}, {"values.question": "How old..?"}]})

然后我意识到这个查询is not correct,我必须使用$elemMatch。但似乎$elemMatch在流星中是unsupported projection

如何更改我的查询或集合以使用oplog,并使我的集合变得简单?

1 个答案:

答案 0 :(得分:0)

一个更好的模型是将问题的答案分开:

{
  formId: "idOfForm",
  values: [
    {_id: "id1", question: "What is your name?"},
    {_id: "id2", question: "How old..?", answer:"7"},
    {_id: "id3", question: "Hobbies?", answer: ["Kitap Okumak", "Spor Yapmak"]},
    {_id: "id4", question: "Foo", answer: "Lorem ipsum ..."}
  ]
}

并在Answers集合中

{_id: "answer1", formId: "idOfForm", questionId: "id1", answer:"Aykut Yaman"}
... and so on 

这样可以节省一些磁盘空间,因为您不会为每个答案存储问题。您不必为每个答案存储formId,但它可能会提高查询表单的性能。