These are chat archives for Automattic/mongoose

9th
Feb 2016
Valeri Karpov
@vkarpov15
Feb 09 2016 14:10
@jhyland87 you've run into one of the more fundamental restrictions of mongodb. The way to work around this is to denormalize and store attributes._fieldin the top level collection as well as the assets collection. This duplicates data and makes writing data a little slower, but makes searching as fast as it can be
antonioaltamura
@antonioaltamura
Feb 09 2016 15:19
guys I have tons on collections, so in my routes I have to repeat many times the POST (save) and GET (get :) ) responses. Maybe there is a way to set a static method for ALL models like list(), getById(), deleteById() ? what's your pratice?
J
@jhyland87
Feb 09 2016 16:07
@vkarpov15 Hold on, so you're saying I should write the data to two locations?..
J
@jhyland87
Feb 09 2016 16:47
@vkarpov15 Instead of storing the data in two places (meaning all inserts/updates/deletes need to be done twice, and kept in sync), wouldnt it be better to just... Select ALL possible documents, then programmatically get the correct document by filtering them out?
The select results would be much bigger... but that might be better than data duplication (which I try to stay away from, wherever possible)
J
@jhyland87
Feb 09 2016 19:03
This message was deleted
This message was deleted
Valeri Karpov
@vkarpov15
Feb 09 2016 19:39
@alfredopacino plugins! We'll support a way to register global plugins at some point: Automattic/mongoose#3732 . Here's a guide to plugins on my blog: http://thecodebarbarian.com/2015/03/06/guide-to-mongoose-plugins
@jhyland87 not necessarily, depends on what you're concerned with. If you duplicate data, you sacrifice write performance, but gain fast read performance and developer sanity. Searching by foreign field is dirt slow in every database I've ever seen, but if you bring the field in you can leverage indexing properly and make search snappy
J
@jhyland87
Feb 09 2016 21:01
Quick question. Lets say I had a model that had a schema with the item 'assetRevision'. and I wanted to get the 2nd to last latest revision.. Is there a way to find that? I was able to find the latest by doing Model.findOne().sort({ assetRevision: 'desc' }).limit(1), but that gets the very latest revision created (which is the current),
@vkarpov15 you might know if thats possible
I know I could limit 2, then just get the 2nd document, but was hoping there was a way to simplify that
Oh, .skip maybe
Hah, score! Model.findOne().sort({ assetRevision: 'desc' }).skip(1).limit(1)
Valeri Karpov
@vkarpov15
Feb 09 2016 23:45
Yep skip() works :)