i m using nodejs (v0.8.15) and latest mongoose
so, i have collection with messages from chat
{ message: String, date: Date, ... }
i need make a query that removes old messages and leave the newest messages, for example 30.
example: database has 50 messages, so when user enters the chat he will remove 20 old and leave 30 newer;
100 messages -> 70 remove, 30 leave;
20 messages -> 0 remove, 20 leave
maybe the only way is to make method or static function, but i m not sure.
thank you.
You can try something like this:
and then use it like that:
Make sure to set index on
dateto increase.sortperformance.