MongoDB user Praise function theory realization [reprint]

Source: Internet
Author: User
Tags findone mongodb

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]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.