These are chat archives for Automattic/mongoose

Apr 2018
Roy Shao
Apr 24 2018 04:19
userSchema.path('username').validate(value => uniqueValidator('User', userSchema, 'username', value), '{VALUE} already exist’);
And when updating, I specify { runValidators: true },
Apr 24 2018 07:51
Can anyone give me ideas how can i update a user email and this changes should affect documents that have their email? Because users can post products and their email is part of this data to identify who posted it. If a user updates his email i want the email in the product data to also update.. i am lost pls help.
Apr 24 2018 08:57
@ycshao maybe add another condition to the count, something like Model.count({ $and: [ { _id: { $not: { $eq: this._id } } }, { path: value } ] }); that way it will never include the current doc in the count.
@2HexGFX_twitter a post save hook on the user Schema that does an updateMany on the products model?
Apr 24 2018 09:19
@lineus can you give me an example? i am not advanced with mongoose yet i will highly appreciate it. Do i have to make a new instance of the user and then do for that instance??
I only understand .save for creating new instance...
Apr 24 2018 09:26
Do you mean pre save or it's really post? i'm trying to find it.
Apr 24 2018 09:29
giving it some more thought, middleware probably isn't the right way to go. With a pre hook, you'll have the old email but no guarantee that the email save will work. With a post hook, you'll only have the new email. Instead, you can add a call to ProductModel.updateMany({ email: oldemail }, { email: newemail}) in the save callback/then.
Apr 24 2018 10:18
@2HexGFX_twitter you should rethink how you are doing that. You should leave the email with the user document only. Keep a userid as a reference in the product instead. This way the userid will not change but the email can and you avoid issues where the email is changed in the user but it fails to update in the other records.
You can use populate to get the email from the product. See here for example.
Apr 24 2018 10:27
@panigrah @lineus thank you, i will try these solutions
Apr 24 2018 10:29
fwiw @2HexGFX_twitter I agree with @panigrah. First choice would be to normalize the data. Barring that, an updateMany call should satisfy the immediate need.
A. Matías Quezada
Apr 24 2018 13:10

hi! I have a stupid question about mongoose
I have a collection of documents with a value property, I just want to multiply that by 0.75 and then ceil it.

I'm trying with Model.update({}, { $mul: { value: 0.75 }})but then I don't know how to round it.
I've seen aggregate allows to pipe operations but I don't understand if the output of aggregation is written in the DB or just returned, I need it to be updated in the DB.

other solution would be to fetch all models, update them, and then save all of them but I don't know how to run a bulk save
Konstantin Kostiuk
Apr 24 2018 18:53
hi. Is it possible to ignore dupe errors on insertMany?
I have to insert large ammount of data, and if some keys alriedy exist, i want just ignore them. Instead - it trow an error and all the rest documents are not inserted
what are the possible workaroud of this?