These are chat archives for Automattic/mongoose

10th
Jun 2015
Erinç Fırtına
@EricMcRay
Jun 10 2015 16:39
@vkarpov15 yeah It is fixed. But now I have another question :D I have field in my schema which uniqueness option true. When I create a new document which is not unique I got mongo error so I need to put mongoose validation for that field again to get Validation Error. Is there an other way? Because every new document I need to check all docs in db. Is there any performance issue about this?
Valeri Karpov
@vkarpov15
Jun 10 2015 16:52
The preferred way is to deal with the mongoerror - there's really no way for mongoose to enforce a uniqueness constraint without mongod's help in the general case at the moment. However, if you have just one app server and aren't really concerned about the race conditions, you can write a pre('save') middleware that first queries the db and checks for any matching docs
if you have an index in place this should be fast, but again it has a race condition. I'd still recommend creating a unique index on the mongod side, either using mongoose schema unique property or manually, and just handling the mongoerror if it happens
Erinç Fırtına
@EricMcRay
Jun 10 2015 17:14
I use both for now. So, I need to handle mongo errors too now. Thanks for quick response
Eddie J
@EddieOne
Jun 10 2015 18:08
Oh git chat cool :3