S
S
Stanislav2017-01-24 00:39:37
MongoDB
Stanislav, 2017-01-24 00:39:37

How to reset the counter every day?

It is required to reset the counter every new day and I don’t understand where to dig and how to implement this idea, I washed it, but I think there may be a more interesting way.

{
    _id: $id,
    count: 100
}

It is necessary to set the count field to 0 every day.
Only crontab comes to mind, once a day, all records have a counter.
I wonder if there are any more elegant options, for example, built into the monge itself? For example, if you create an additional field with the time of the last reset of the count field, and when updating, changing the document, check the field and reset it? Maybe there are some built-in things with a field update interval that I don't know about?
Or is it only for hardcore crontab? =)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene Wolf, 2017-01-24
@ms-dred

Or is it only for hardcore crontab? =)
This is not hardcore, but one of the practices (one of the approaches).
For example, if you create an additional field with the time of the last reset of the count field, and when updating, changing the document, check the field and reset it?
Можете завести дополнительное поле с датой, и если счётчик != 0 и дата == сегодня, и время обновления >= "уже пора", сбрасываете счётчик на 0 и увеличиваете дату на 1 день. Но по моему, каждый раз дёргать документ, что бы проверить дату и вообще так извращаться, как-то не серьёзно, особенно если таких проверок в день у вас будет не 10 и не 20... (если мало, то особо не принципиально)
P.S. В MySQL есть планироващик свой, на уровне БД. В Монге я такого не припоминаю...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question