These are chat archives for Automattic/mongoose

4th
Nov 2018
Maurizio Bellemo
@mbellemo_twitter
Nov 04 2018 10:50
@lineus back to the original endpoint, now i have a different question.
I would like to add a sorting to the endpoint depending on the length of the embedded upvotes array. How would you do it?
router.get('/:id/feedbacks/', authenticate(['user']), (req, res) => {
    const filter = {
        company_id: req.user.company_id,
        project_id: req.params.id
    };
    if (req.query.search) {
        filter.$or = [
            { title: { $regex: req.query.search, $options: 'i' } },
            { description: { $regex: req.query.search, $options: 'im' } }
        ];
    }

    const perPage = 25
    const page = req.query.page || 1

    Feedback.find(filter)
        .skip((perPage * page) - perPage)
        .limit(perPage)
        .then((feedbacks) => {
            Feedback.countDocuments(filter)
                .then((count) => {
                    res.send({
                        feedbacks,
                        current: page,
                        pages: Math.ceil(count / perPage),
                        total: count
                    });
                })
        }, (e) => {
            res.status(400).send(e);
        });
Maurizio Bellemo
@mbellemo_twitter
Nov 04 2018 11:13
I tried to find an answer to my question and I discovered the aggregate function in Mongoose
router.get('/:id/feedbacks/', authenticate(['user']), (req, res) => {
    const filter = {
        company_id: new ObjectID(req.user.company_id),
        project_id: new ObjectID(req.params.id)
    };

    if (req.query.search) {
        filter.$or = [
            { title: { $regex: req.query.search, $options: 'i' } },
            { description: { $regex: req.query.search, $options: 'im' } }
        ];
    }

    let sort = { "length": -1 };

    if(req.query.sort) {
        if(req.query.sort == "recent") {
            sort = { "updatedAt" : -1 };
        }
        else if (req.query.sort == "real") {
            sort = { "real" : -1 };
        }
        else if (req.query.sort == "potential") {
            sort = { "potential" : -1 };
        }
    }

    const perPage = 25
    const page = req.query.page || 1

    Feedback.aggregate(
        [
            {
                "$project": {
                    "title": 1,
                    "description": 1,
                    "size": 1,
                    "potential": 1,
                    "real": 1,
                    "updatedAt": 1,
                    "company_id": 1,
                    "project_id": 1,
                    "owner_id": 1,
                    "length": { "$size": "$upvotes" }
                }
            },
            { "$match": filter },
            { "$sort": sort },
            { "$skip": (perPage * page) - perPage },
            { "$limit": perPage }
        ],
        function (err, feedbacks) {
            if(err) {
                res.status(400).send(e);
            } else {
                console.log(feedbacks);
                Feedback.countDocuments(filter)
                .then((count) => {
                    res.send({
                        feedbacks,
                        current: page,
                        pages: Math.ceil(count / perPage),
                        total: count
                    });
                })
            }
        }
    )
});
Ghost
@ghost~5928d90bd73408ce4f629b9e
Nov 04 2018 12:10
hello how should i do multiple updates with express and mongoose, say in front-end i have a setting and i can update name, number and age. I can update only one or all of them when i click 'save changes' .. but how do i handle that in backend if i don't know which will be updated? currently i am doing if statement to see which will be updated but it is not pretty and i might need to add more setting, can i have idea please
Berian
@chaiwa-berian
Nov 04 2018 19:35
Hi everyone! Do I need to create a separate set of update validators for the concept of update validators to work? I mean, I have already defined validations on the schema but whenever I update the document, the validations do not get fired despite passing the runValidators: true option to the findOneAndUpdate() method
Berian
@chaiwa-berian
Nov 04 2018 19:42
@2HexGFX_twitter I guess you can simply use one of the update methods and mongoose/mongodb will take care of the partial updates, like:
YourModel.findOneAndUpdate({_id: req.params.id}, req.body, function(err, doc){
    if(err) 
      {
        ....
     }
    return res.status(200).send(doc)
     }, {new: true}
)
Berian
@chaiwa-berian
Nov 04 2018 20:49

Hi everyone! Do I need to create a separate set of update validators for the concept of update validators to work? I mean, I have already defined validations on the schema but whenever I update the document, the validations do not get fired despite passing the runValidators: true option to the findOneAndUpdate() method

I added the {context: 'query'} and it worked though I do not fully understand why it works! :)