These are chat archives for Automattic/mongoose

28th
Jun 2015
Fayi Femi-Balogun
@fayimora
Jun 28 2015 08:37
I have a pre-save hook that inits and updates my createdAt and updatedAt fields. Problem is i have to duplicate the logic for each Schema. Is there a way to have this hook for all schemas?
Erinç Fırtına
@EricMcRay
Jun 28 2015 08:39
make it like mongoose plugin and add that to all your schemas?
Fayi Femi-Balogun
@fayimora
Jun 28 2015 08:40
@EricMcRay sorry are you asking if that’s what I want? or suggesting a way to go about it?
Erinç Fırtına
@EricMcRay
Jun 28 2015 08:41
@fayimora suggesting a way :D
Fayi Femi-Balogun
@fayimora
Jun 28 2015 08:41
oh ok thanks. I’ve always wondered wat mongoose plugins were. I bet one for this taksk might exist
Still requires duplicating across schemas but at least i’m duplicating less code
Fayi Femi-Balogun
@fayimora
Jun 28 2015 08:46
Is there a way to create my own ProjectSchema that other schemas inherit from? That way I can just have the timestamp "logic" in one place
Erinç Fırtına
@EricMcRay
Jun 28 2015 08:50
@fayimora yes you can extend mongoose schema but that can be little complicated. google “object orianted javascript” you can find lot of tutorials about how you can code class like javascript and extend that classes.
Fayi Femi-Balogun
@fayimora
Jun 28 2015 08:51
“but that can be little complicated” <= i thought as much. I’ll stick to the duplication for now lol
Fayi Femi-Balogun
@fayimora
Jun 28 2015 08:52
thanks Eric
Erinç Fırtına
@EricMcRay
Jun 28 2015 08:54
@fayimora you welcome I think mongoose plugin will be best options. Yes you need to add that plugin to all your schemas but you can change your code from one place.
Eric Lorentz
@unholyknight
Jun 28 2015 20:38
I want to track and return a sum of a certain piece of data from all documents (around 2,000) of a given type, are aggregations the correct way to go about this? Still new to NoSQL and want to make sure I'm handling this the correct way.
Erinç Fırtına
@EricMcRay
Jun 28 2015 21:46
Yeap
Eric Lorentz
@unholyknight
Jun 28 2015 21:47
Thanks!