Stores an array of _id for the user who likes the article in the posts (article) collection, for example:
posts{ _id:objectid (' 4e7020cb7cac81af7136236b '), users_like_this_post: [ ObjectID (' 4e7020cb7cac81af71362361 '), ObjectID (' 4e7020cb7cac81af71362362 ') }
Check an article that likes the user:
Post = Db.posts.findOne ({_id:objectid (' 4e7020cb7cac81af7136236b ')}); Console.log (Post.users_like_this_post);
Check an article for the number of likes:
Post = Db.posts.findOne ({_id:objectid (' 4e7020cb7cac81af7136236b ')}); Console.log (post.users_like_this_post.length );
Posts that have liked 100:
Posts = Db.posts.find ({' users_like_this_post.100 ': {$exists: true}});
Check the user-liked article:
Posts = Db.posts.find ({users_like_this_post:user._id});
User likes to post:
Db.posts.update ({_id:post._id}, {$addToSet: {users_like_this_post:user._id}});
User likes to post cancel:
Db.posts.update ({_id:post._id}, {$pull: {users_like_this_post:user._id}});
参考 https://segmentfault.com/q/1010000000663821
MongoDB user Praise function theory realization [reprint]