How to use 'like query in mongodb' in JavaScript

Every line of 'like query 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.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
651public 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}
155parseQuery(ctx, query) {
156 return this.mongoQueryParser(ctx, query);
157}

Related snippets