Every line of 'like in mongodb' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.
651 public async like(param: any): Promise { 652 const { id: _id } = param 653 const userId = _.get(this.currentUser, '_id') 654 const doc = await this.model.findById(_id) 655 const { likes, dislikes } = doc 656 657 // can not both like and dislike, use ObjectId.equals to compare 658 if (_.findIndex(dislikes, oid => userId.equals(oid)) !== -1) return doc 659 660 // already liked, will unlike, use ObjectId.equals to compare 661 if (_.findIndex(likes, oid => userId.equals(oid)) !== -1) { 662 await this.model.findOneAndUpdate( 663 { _id }, 664 { 665 $pull: { likes: userId }, 666 $inc: { likesNum: -1, activeness: -1 } 667 } 668 ) 669 } else { 670 // not like yet, will like it 671 await this.model.findOneAndUpdate( 672 { _id }, 673 { 674 $push: { likes: userId }, 675 $inc: { likesNum: 1, activeness: 1 } 676 } 677 ) 678 } 679 680 return this.model.findById(_id) 681 }
70 async function likeATopic (topicId) { 71 console.log(topicId); 72 const topic = await TopicModel.findOneAndUpdate({_id: topicId}, {$inc:{likes:1}}, {new: true, fields: {likes:1}}) 73 return topic.likes 74 }