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:
{
"_id":"5d891d2597d15c4ada73d4e0",
"board_title":"general",
"threads":
[
{
"text":"This is the first general thread",
"delete_password":"123",
"created_on":"2019-08-26T17:24:36.496Z",
"bumped_on":"2019-08-26T17:24:36.496Z",
"reported":false,
"replycount":1,
"replies":
[
{
"_id":"5d89d88ff08c8c47b0f83231",
"text":"This is a repy to general",
"delete_password":"456",
"created_on":"2019-08-26T17:24:36.496Z",
"reported":false
}
],
"_id":"5d89d85cf08c8c47b0f83230"
},
{
"text":"Second general thread",
"delete_password":"123",
"created_on":"2019-08-24T17:24:36.496Z",
"bumped_on":"2019-08-28T17:24:36.496Z",
"reported":false,
"replycount":8,
"replies":
[
{"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"}
],
"_id":"5d8a0683f08c8c47b0f83232"
}
]
}
Then this doesn’t work
Board.findOne({board_title: boardTitle})
.select("-delete_password -reported")
.limit(5)
.sort({'threads.bumped_on': -1})
.exec(function (err, docs) { });