Reputation: 1397
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
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