These are chat archives for Automattic/mongoose

2nd
May 2018
Vlado Tesanovic
@vladotesanovic
May 02 2018 06:54
@donald-s You can write your own validation function
1) Get properties from schema
2) Filter out undesired fields
3) save to db
Ghost
@ghost~5928d90bd73408ce4f629b9e
May 02 2018 07:18

I am lost on how i would handle duplicate entries
I want to check if the email or display name is already in used.

If i enter an existing email and an existing name only the data of user that was math via email is return and not the existing name. Shouldn't both match?

// request data are both existing in the database
 Account.find({$or: [ {user_email: req.body.email}, {display_name: req.body.name}]}) 
        .exec((err, user) => {
            if(err) { 
                res.status(500).json({message: 'Internal Server Error', type: 'error'});
            }

            if(user) {
                 //  Only the data from the email match result is logged e.g { _id: 5ae6cd68e1f3b32237494792, user_email: 'bob@gmail.com' } 
                 console.log(user)
                // Response
                res.json({
                    message: 'Account email address or Display name is already in use',
                    type: 'error'
                })
            } else {
                next();
            }
        });
    }

Also how would send a response with the correct error like Display name is already in use or Email is already in use instead of Account email address or display name is already in use which is very implicit

I am thinking of using separate db call with promise but is there a more mongoose way of doing this?

Kev
@lineus
May 02 2018 10:54
@vladotesanovic Model.bulkWrite() will cast values, but it doesn't trigger middleware ( including validation )
Kev
@lineus
May 02 2018 11:56
@donald-s you'll want to ensure that the update object in each command passed to Model.bulkWrite() only contains the fields you want to update. This will have to happen before calling bulkWrite. The benefit of only one db call provided by Model.bulkWrite() comes at the cost of losing some of mongoose's protections/functionalities.
@donald-s here is a gist with a basic function to remove keys from the update object of updateOne commands.
Kev
@lineus
May 02 2018 12:27
@2HexGFX_twitter you can create a unique index on those two paths which will cause mongodb to throw an E11000 error when attempts to input duplicate values are detected. Then you just have to handle the error. The error returned will have the path that failed: gist here
panigrah
@panigrah
May 02 2018 13:37
@2HexGFX_twitter unclear what you are trying to do. Your condition could return multiple records, one where the email matches and another where the name matches. So user will actually be an array if there is more than one item found. Perhaps the first record you are finding is the record with the email.
If you are saying that the console.log(user) is only printing the _id and the user_email and nothing else, perhaps your document does not store the display_name in the same schema or not all the user documents have display_name stored.
Don Schrimsher
@donald-s
May 02 2018 17:34

@vladotesanovic @lineus Thanks for the help. I greatly appreciate it. @lineus Thanks a bunch for taking the time to put together that gist. I figured manually filtering out the extra keys / values using a function was the only approach - which I actually implemented yesterday. However, I did it by decorating the bulkWrite method. Here's a gist for anyone interested. Note that I'm currently only doing this for the $set operator.

Do you guys think there'd be any interest in a PR for adding validation to bulkWrite? I feel like this would be useful to a lot of other people and would make things consistent (since this can be done for insertMany). From what I've seen too, I don't think it would be that hard.