These are chat archives for Automattic/mongoose

13th
Aug 2018
sm2017
@sm2017
Aug 13 2018 04:49
I read https://docs.mongodb.com/v3.4/tutorial/perform-two-phase-commits/ and want to implement it , how can I run the following queriy with mongoose?
db.transactions.update(
    { _id: t._id, state: "initial" },
    {
      $set: { state: "pending" },
      $currentDate: { lastModified: true }
    }
)
How can I have a filter for update ?
Kev
@lineus
Aug 13 2018 10:30

@sm2017 the first parameter is the condtional ( aka filter, or selector ) object in both the native driver's update method and the mongoose update method.

If you named your mongoose Model "Transaction" with:

const Transaction = mongoose.model('transaction', transactionSchema);

then the equivalent of the above command would be:

Transaction..update(
    { _id: t._id, state: "initial" },
    {
      $set: { state: "pending" },
      $currentDate: { lastModified: true }
    }
)
Maurizio Bellemo
@mbellemo_twitter
Aug 13 2018 11:40

Hi all. I have a question about our updating embedded object works.
I have a document, called Customer, that stores, as embedded array, a list of deals. When I trie to update one deal of the array, the object gets correctly updated including unfortunately the _id property (a new one is generated). Why? I only want to update the "real" properties of the object not the autogenerated id

router.patch('/:id/deals/:dealId', authenticate(['user', 'admin']), (req, res) => {
    const customerId = req.params.id;
    const dealId = req.params.dealId;
    const body = _.pick(req.body, ['name', 'status', 'type', 'from', 'to', 'value']);

    if (!ObjectID.isValid(customerId) || !ObjectID.isValid(dealId)) {
        return res.status(404).send();
    }

    Customer.findOneAndUpdate(
        { _id: customerId, company_id: req.user.company_id, "deals._id": dealId },
        {
            $set: {
                'deals.$': body
            }
        },
        { new: true }).then((customer) => {
            if (!customer) {
                return res.status(404).send();
            }
            res.send({ customer });
        }, (e) => {
            res.status(400).send();
        });
});

Any idea how to solve it?

Kev
@lineus
Aug 13 2018 14:00
@mbellemo_twitter when you set the entire subdoc deals.$ to a new object that doesn't contain the _id, mongoose is adding a new _id path for you. here's a gist that shows a couple of ways around this
TL;DR - pass the _id in with the update object :)
André Bomark
@ZwiFTi
Aug 13 2018 18:50
So i created a min and max validator difficulty: { type: Number, default: 0, min: 0, max: 10 }, The problem is that i can still lower the difficulty below 0, why is this? Also, would it be better to have the validator in my game logic instead so I dont have to go through the database?
Kev
@lineus
Aug 13 2018 19:13
@ZwiFTi I don't think the min/max validators run on update calls. If you want to keep the logic in mongoose you can create a static method that grabs the current value of the field in the db and only updates it if it matches your expectations. example here
Kev
@lineus
Aug 13 2018 19:19
or you can try to remember to always call doc.save() when you need the min/max validator like in this example
Maurizio Bellemo
@mbellemo_twitter
Aug 13 2018 20:21
@lineus thanks! You saved my day :)
Kev
@lineus
Aug 13 2018 20:27
glad I could help @mbellemo_twitter :smile: