2016-02-05 41 views
0

我正在研究将用于报告的原型(只读),其中记录是嵌入到单个文档中的一组非常丰富的对象。本质上,文档结构是这样的(编辑为简洁起见):将Mongoose与丰富的文档结合使用?

{ 
    "_id": ObjectId("56b3af6f84ef45c8903acc51"), 
    "id": "7815dd97-e895-46e5-b6c9-45184c6eae89", 
    "survey": { 
    "id": "1fb21c69-6a5c-4805-b1cf-fabef7a5d0e6", 
    "type": "Survey", 
    "data": { 
     "description": "Testing reporting and data ouput", 
     "id": "1fb21c69-6a5c-4805-b1cf-fabef7a5d0e6", 
     "start_date": "2016-02-04T11:12:46Z", 
     "questions": [ 
     { 
      "sequence": 1, 
      "modified_at": "2016-02-04T16:11:04.505849+00:00", 
      "id": "2a77921b-6853-463b-80e7-5713c82c51ca", 
      "previous_question": null, 
      "created_at": "2016-02-04T16:10:56.647746+00:00", 
      "parent_question": "", 
      "next_question": "", 
      "validators": [ 
      "required", 
      "email" 
      ], 
      "question_data": { 
      "modified_at": "2016-02-04T16:10:37.542715+00:00", 
      "type": "open-ended", 
      "text": "Please provide your email address", 
      "id": "27aa00db-4a56-4a3e-bc30-226179062af0", 
      "reporting_name": "email address", 
      "created_at": "2016-02-04T16:10:37.542695+00:00" 
      } 
     }, 
     { 
      "sequence": 2, 
      "modified_at": "2016-02-04T16:09:53.539073+00:00", 
      "id": "c034819d-9281-4943-801f-c53f4047d03e", 
      "previous_question": null, 
      "created_at": "2016-02-04T16:09:53.539051+00:00", 
      "parent_question": "", 
      "next_question": null, 
      "validators": [ 
      "alpha-numeric" 
      ], 
      "question_data": { 
      "modified_at": "2016-02-04T16:05:31.008363+00:00", 
      "type": "open-ended", 
      "text": "Is there anything else that we could have done to improve your experience?", 
      "id": "e33c7804-20cb-4473-abfa-77b3c2a3113c", 
      "reporting_name": "more info open-ended", 
      "created_at": "2016-02-01T20:19:55.036899+00:00" 
      } 
     }, 
     { 
      "sequence": 1, 
      "modified_at": "2016-02-04T16:08:55.681461+00:00", 
      "id": "f91fd70e-f204-4c38-9a56-dd6ff25e4cd8", 
      "previous_question": "", 
      "created_at": "2016-02-04T16:08:55.681441+00:00", 
      "parent_question": "", 
      "next_question": null, 
      "validators": [ 
      "required" 
      ], 
      "question_data": { 
      "modified_at": "2016-02-04T16:04:56.848528+00:00", 
      "type": "nps", 
      "text": "On a scale of 0-10 how likely are you to recommend us to a friend?", 
      "id": "fdb6b74d-96a3-4680-af35-8b2f6aa2bbc9", 
      "reporting_name": "key nps", 
      "created_at": "2016-02-01T20:19:27.371920+00:00" 
      } 
     } 
     ], 
     "name": "Reporting Survey", 
     "end_date": "2016-02-11T11:12:47Z", 
     "trigger_active": false, 
     "created_at": "2016-02-04T16:13:16.808108Z", 
     "url": "http://www.peoplemetrics.com", 
     "fatigue_limit": "monthly", 
     "modified_at": "2016-02-04T16:13:16.808132Z", 
     "template": { 
     "id": "0ea02379-c80b-4e17-b0a6-d621d49076b9", 
     "type": "Template" 
     }, 
     "landing_page": null, 
     "trigger": null, 
     "slug": "test-reporting-survey" 
    } 
    }, 
    "invite_code": "7801", 
    "end_date": null, 
    "created_at": "2016-02-04T19:38:31.931147Z", 
    "url": "http://127.0.0.1:8000/api/v0/responses/7815dd97-e895-46e5-b6c9-45184c6eae89", 
    "answers": { 
    "data": [ 
     { 
     "id": "bcc3d0dd-5419-4661-9900-ccda3ac9a308", 
     "end_datetime": "2016-01-22T19:57:03Z", 
     "survey_question": { 
      "id": "662fcdf9-3c92-415e-b779-ac5b0fd330d3", 
      "type": "SurveyQuestion" 
     }, 
     "response": { 
      "id": "7815dd97-e895-46e5-b6c9-45184c6eae89", 
      "type": "Response" 
     }, 
     "modified_at": "2016-02-04T19:38:31.972717Z", 
     "value_type": "number", 
     "created_at": "2016-02-04T19:38:31.972687Z", 
     "value": "10", 
     "slug": "", 
     "start_datetime": "2016-01-21T10:10:21Z" 
     }, 
     { 
     "id": "8696f11e-679a-43da-b6e2-aee72a70ca9b", 
     "end_datetime": "2016-01-28T13:45:37Z", 
     "survey_question": { 
      "id": "f118c9dd-1c03-47e0-80ef-2a36eb3b9a29", 
      "type": "SurveyQuestion" 
     }, 
     "response": { 
      "id": "7815dd97-e895-46e5-b6c9-45184c6eae89", 
      "type": "Response" 
     }, 
     "modified_at": "2016-02-04T19:38:32.001970Z", 
     "value_type": "boolean", 
     "created_at": "2016-02-04T19:38:32.001939Z", 
     "value": "True", 
     "slug": "", 
     "start_datetime": "2016-02-15T04:51:24Z" 
     } 
    ] 
    }, 
    "modified_at": "2016-02-04T19:38:31.931171Z", 
    "start_date": "2016-02-01T16:14:13Z", 
    "invite_date": "2016-02-01T13:14:08Z", 
    "contact": { 
    "id": "94833455-b9b8-4206-9bc9-a2f96c1706ca", 
    "type": "Contact", 
    "external_contactid": null, 
    "name": "Miss Marceline Herzog PhD" 
    }, 
    "referring_source": "web" 
} 

给出了该格式的结构,我是最好的前进道路使用猫鼬的ORM不确定。再说一次,这是只读的,所以我认为创建一个嵌套模式是可行的,但映射本身似乎很乏味。是否有更好的/不同的选项可用于嵌入式内容?

回答

1

有趣。首先,我会考虑是否需要所有文档及其嵌入的子文档字段。你说它是只读的,每次调用都需要整个文档?

如果不是,我建议看看mongo drivers(node.js,.NET,Python等)并使用它们的aggregation pipelines来简化文档,如果可能的话。

如果您使用的是Mongoose,您最终可能会有两个或三个Schema,并且在模式列表中有模式。 Mongoose docs例如

var surveySchema = new Schema(
    { "type"  : string, 
     "data"  : [dataSchema], 
    "invite_code" : string, 
    "end_date"  : DateTime, 
    "created_at" : DateTime, 
    "url"   : string, 
    "answers"  : { "data": [answersSchema]}, 
    "modified_at" : DateTime, 
    "start_date" : DateTime, 
    "invite_date" : DateTime, 
    "contact"  : [ContactSchema], 
    "referring_source" : string 
}); 

或者,你可以使用猫鼬references并根据您需要使用您的报告中哪些数据建立自己的模式。一个简单的例子:

var surveySchema = { 
      "id"  : { type: Schema.Types.ObjectId } 
    "description" : { type: string , ref: dataSchema }, 
    "contactSchema" : { type: string , ref: contactSchema } 
} 
+0

这些基本上是完整的图表文件,其中报告在响应以及相关调查对象之间被拆分。这可能是因为这个文档需要分解成相关的部分,并以这种方式存储在Mongo中,但我希望我可以保持现在的状态,并使用Mongoose进行简单的列表和图形(现在的演示)。 – BryanGrimes