mongodb - Using Mongoose with a rich document? -
i'm working on prototype used reporting (read only) record rich set of objects embedded single document. document structure (edited brevity):
{ "_id": objectid("56b3af6f84ef45c8903acc51"), "id": "7815dd97-e895-46e5-b6c9-45184c6eae89", "survey": { "id": "1fb21c69-6a5c-4805-b1cf-fabef7a5d0e6", "type": "survey", "data": { "description": "testing reporting , 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 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 else have done improve 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 scale of 0-10 how recommend 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" }
given structure in format, i'm unsure best path forward using mongoose orm. again, read-only, seem creating nested schema work, mapping seems tedious least. there better/different option available embedded?
interesting. first, think if need document , embedded subdocuments fields. said read-only, each call needs entire document?
if not, recommend taking @ mongo drivers (node.js, .net, python, etc.) , using aggregation pipelines simplify document if possible.
if you're using mongoose, end 2 or 3 schemas, , schemas inside list. mongoose docs e.g.
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 });
or, can use mongoose references , build own schema depending on data need use report. simple example:
var surveyschema = { "id" : { type: schema.types.objectid } "description" : { type: string , ref: dataschema }, "contactschema" : { type: string , ref: contactschema } }
Comments
Post a Comment