Collection per object or document per object

I am on my final Information Security and Quality Assurance Project and find myself in the same situation as most of my other projects involving MongoDB.

I am in sub document hell - arrays inside objects inside objects…etc.

This limits my use of .sort(), .limit() etc when using Mongoose findOne() as I can’t sort by fields in subarrays.

I think my issue is that I create ONE collection and then created complex documents inside this one collection.

This is my Question…(for this project anyway):
is it better to create a new collection for each Board (top level Object) and then everything (arrays, etc) move up one level rather than a document per object in the same collection?

For example:

			"text":"This is the first general thread",
					"text":"This is a repy to general",
			"text":"Second general thread",
				{"text":"second	thread reply1","delete_password":"456","created_on":"2019-08-26T17:24:36.496Z","reported":false,"_id":"5d8a06a5f08c8c47b0f83233"},
				{"text":"second thread reply2","_id":"5d8a06b3f08c8c47b0f83234","delete_password":"456","created_on":"2019-08-26T17:24:36.496Z","reported":false},
				{"text":"second thread reply3","_id":"5d8a06bef08c8c47b0f83235","delete_password":"456","created_on":"2019-08-26T17:24:36.496Z","reported":false},
				{"text":"second thread reply4","_id":"5d8a06ccf08c8c47b0f83236","delete_password":"456","created_on":"2019-08-30T17:24:36.496Z","reported":false},
				{"text":"second thread reply5","_id":"5d8a06d5f08c8c47b0f83237","delete_password":"456","created_on":"2019-08-31T17:24:36.496Z","reported":false},
				{"text":"second thread reply6","delete_password":"456","created_on":"2019-09-01T17:24:36.496Z","reported":false,"_id":"5d8a06e0f08c8c47b0f83238"},
				{"text":"second thread reply7","delete_password":"456","created_on":"2019-09-02T17:24:36.496Z","reported":false,"_id":"5d8a06e9f08c8c47b0f83239"},
				{"text":"second thread reply8","delete_password":"456","created_on":"2019-08-26T17:24:36.496Z","reported":false,"_id":"5d8a06f4f08c8c47b0f8323a"}

Then this doesn’t work

Board.findOne({board_title: boardTitle})
        .select("-delete_password -reported")
        .sort({'threads.bumped_on': -1})       
        .exec(function (err, docs) { });

Maybe find a way to write your code more effecienter ? Give a look into effectiver writing in code.Your sorta writting allot out that could be handled “cleaner”

do you mean the database design?

Yess!!! I see allot of people doing this who start out including myself. Writting huge lines of codes writting everything out.

1 Like

ok. I’ll read more into database design.

And see if I can be more like MongoDB and less relational like SQL

I’ve been reading…

I understand now that I have a one to many relationship between my

  • Board and Threads
  • Thread and Replies

And it might be a good option to…

create separate collections for my threads and replies and have Two-Way Referencing by their ObjectID. There will be more data reading and less data updating.

Is this a more effective (cleaner) approach to a database design, @KittyKora
Am I on the right track?

1 Like

It’s a good start for sure :3 keep it up but, also do something you feel comfertable using. I love hearing more from you.

Also what I am working myself with in this moment is a good planning alway trying to documenting effectively, create a good naming system and a clean desk helps me a bunch.

I just wanted you to know that I took your advice, read up some more on try/catch, mongoose promises, populate(), etc.

I redesigned my database schemas and I’ve now completed the Anonymous Message Board.
Once I was able to easily get the data I needed it was very easy to complete the project. Not very much data manipulation was needed.

Thanks again!