Daryl Rodrigo
Daryl Rodrigo

Reputation: 1397

How do you return the updated object when updating in mongodb?

I'm trying to get the objectId back when updating by another field in mongo. The code I have at the moment is:

twitterDb.update({'twitterId': t.id},tweetData, {upsert:true}, function(err, twitterDb) {
//do stuff here
});

note that twitterId is different from the objectId.

How do I return the full object so that I can get the objectId?

Thanks in advance!

Upvotes: 1

Views: 1378

Answers (1)

Waldo Jeffers
Waldo Jeffers

Reputation: 2279

Take a look at Mongoose's findByIdAndUpdate. Provided you also know the _objectId of the Document you want to update, I think it should suit your needs.

Otherwise, you will need to find it first, modify it, and save it :

twitterDb.find({'twitterId': t.id}, function (err, user) {
    if (err) return handleError(err);
    user.tweetData = tweetData;
    user.save(function (err) {
        if (err) return handleError(err);
       else
           //...
    });
});

Upvotes: 1

Related Questions