V
V
vostotskiy2018-03-26 20:23:05
JavaScript
vostotskiy, 2018-03-26 20:23:05

How to correctly group by date an array property (comments) in MogoDb?

Good afternoon. I am doing a simple cms on the mean stack, the lack of long experience in Mondo and NoSQL brought me to a small stupor.
There is a collection of articles in which comments are stored as a separate property in the form of an array.
Here is his description.

comments: [{
     message: String,
     postedBy: {type: Schema.Types.ObjectId, ref: 'User'},
      createdDate: {type: Date, default: Date.now, required: true}    }]

I can’t figure out how to group by days in such a way that I can display them in a cycle on the front, separating them by date, and in each message just display the time in hours and minutes.
That is, for example,
10. 01
8:15 post 1
8:50 post 2
12.01
10:00 post 4
15:00 post 4
Perhaps you should store comments as a separate collection and make a request in a completely different way?
At the moment, just for display, I'm pulling out the comments, adding to the request I would be grateful for any advice.
.populate('comments')

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Philipp, 2018-03-26
@zoonman

Use aggregation and grouping by year, month, day.
https://docs.mongodb.com/manual/reference/operator...
Your pipeline will include an $unwind operator that will break up comments.
Then group them by date with the union via $addToSet .
Install Robomongo and watch YouTube about aggregation.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question