zoukankan      html  css  js  c++  java
  • mongodb 用户点赞功能理论实现[转载]

    在 posts(文章) 集合中储存对该文章点赞的用户的 _id 的数组,例如:

    // posts
    {
        _id: ObjectID('4e7020cb7cac81af7136236b'),
        users_like_this_post: [
            ObjectID('4e7020cb7cac81af71362361'),
            ObjectID('4e7020cb7cac81af71362362')
        ]
    }

    查对一个文章点赞的用户:

    post = db.posts.findOne({_id: ObjectID('4e7020cb7cac81af7136236b')});
    console.log(post.users_like_this_post);

    查一个文章的点赞数量:

    post = db.posts.findOne({_id: ObjectID('4e7020cb7cac81af7136236b')});
    console.log(post.users_like_this_post.length);
    

      

    查点赞过 100 的文章:

    posts = db.posts.find({'users_like_this_post.100': {$exists: true}});
    

      

    查 user 点赞过的文章:

    posts = db.posts.find({users_like_this_post: user._id});
    

      

    user 对 post 点赞:

    db.posts.update({_id: post._id}, {$addToSet: {users_like_this_post: user._id}});
    

      

    user 对 post 取消点赞:

    db.posts.update({_id: post._id}, {$pull: {users_like_this_post: user._id}});
    

      

    参考 https://segmentfault.com/q/1010000000663821
  • 相关阅读:
    k8s二进制安装
    jenkins
    Deploy Apollo on Kubernetes
    Apollo配置中心搭建常见报错
    Apollo配置中心搭建过程
    使用CephRBD为Kubernetes提供StorageClass
    Ceph基础命令总结
    Ceph分布式存储系统搭建
    zabbix入门之配置邮件告警
    zabbix入门之定义触发器
  • 原文地址:https://www.cnblogs.com/dupd/p/9062906.html
Copyright © 2011-2022 走看看