Thursday, 11 May 2017

$ projection in mongoDB findOneAndUpdate()

I'm trying to build a simple task queue with express and mongoose. The idea is acquire a single client and return campaign id and client id (which is a subdocument of campaign). Each time someone acquires a client, its status code is set to 1. I've come up with the following query:

router.post('/lease', (err, res) => {
    Campaign.findOneAndUpdate({'isEnabled': true,  'clients.contact_status_code': 0}, {
            '$set': { 'clients.$.contact_status_code': 1 },
        },
        {
            new: true,
            projection: {
                'clients.$': true,
            },
        },
        (err, campaign) => {
            if (err) {
                return res.send(err);
            }

            res.json(campaign);
        }
    );
});

But all i'm getting after connecting to this endpoint is this:

{"_id":"591483241a84946a79626aef","clients":[{},{}]}

It seems to me that the problem is with the $ projection, but I have no idea how to fix this.



via foverzar

No comments:

Post a Comment