0

On the front end, I have 3 levels of hierarchy.

The top level presents a test summary with statistics such as % pass/fail, various tags, run ids etc.

The second level consists of sections of test suites which can be accessed via the top level run id. This contains the names of the tests, which specific ones passed/failed.

The third level is the actual test itself with a report, pass/fail status etc.

I'm using the MEAN stack for this and was wondering about the trade-offs of using 1,2 or 3 different Mongoose models to store data in MongoDB. I know one of the issues with using embedded documents in an array would be that I would get the whole array back in a query/update instead of a specific element.

Option 1)

Use 3 models, one for each summary, section and test. Then use the same run_id as a value on which I can tie them together.

Option 2)

Use 2 models, summary and section. The section then has an array with embedded docs, each related to a specific test.

Option 3)

Only have test and section models and create the summary if a query needs it.

Not sure if this information is enough, let me know and I can try and add more. Thanks

1 Answer 1

1

I'm just quoting you on this statement:

I know one of the issues with using embedded documents in an array would be that I would get the whole array back in a query/update instead of a specific element.

according to the documentation for mongo find http://mongoosejs.com/docs/api.html#model_Model.find

you have these parameters: Parameters:

  • conditions
  • [fields] optional fields to select
  • [options] optional
  • [callback]

you can query and do a projection over whatever n fields you want to retreive and include only the data that you need.

to me most operations in mongo are atomic that's why dealing with mongo it's easier than database, but also you can update single pieces of the document using $set like

Model.where({ _id: id }).update({ $set: { title: 'words' }}) 

just keep in mind that updating a document that have embeded documents are slower by writing but faster for reading and in the other case having separate collection can be the other way around, there's no a straight answer for this, sometimes you'll have to do a trial and error testing and see whatever fits better for your requirements. on you scenario I have the feeling that you could try to use an embedded document and see how it works or you could try option number 2.

I hope that make sense! cheers!

Sign up to request clarification or add additional context in comments.

5 Comments

So if I understand it correctly, I can set a specific field inside a selected element of an embedded document?....That is, lets say I have a section which contains an array of test documents. Can I select only one test out of the section and increment the pass value in it.
yes you retreive only pieces of it and also update single array elements, llok at this answer stackoverflow.com/questions/15691224/…
you can use the $ operator in your updates, and that do the magic for you!
Awesome, thanks! I think I'll be going with a summary model and section model with embedded test docs.
also for doing the queries inside an array embedded document look at stackoverflow.com/questions/17690545/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.