在 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

最新文章

  1. HttpClient, HttpClientHandler, and WebRequestHandler Explained
  2. hg0088新2网址
  3. Jquery cxColor 示例演示
  4. JSP EL表达式(转)
  5. .NET环境下上传和下载Word文件
  6. .net 第二周学习
  7. apache 日志轮询 linux cronolog
  8. 加州大学伯克利分校Stat2.2x Probability 概率初步学习笔记: Section 4 The Central Limit Theorem
  9. linux常用头文件及说明
  10. xsheell的下载安装初级使用
  11. 【转】larbin中的url去重算法
  12. Tick and Tick
  13. 异步执行Dos命令
  14. 在GNU/Linux下设置与定时更换桌面壁纸
  15. UVa 532 - Dungeon Master
  16. jquery checkbox radio 标签 选中的3种方法
  17. redis报错:java.net.SocketException: Broken pipe (Write failed); nested exception is redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketException: Broken pipe (Write failed)
  18. PyEngine3D
  19. pdf转换成word转换器免费版
  20. 使用 dl 设计的简单的登陆界面 (为了记录)

热门文章

  1. 模拟退火算法(run away poj1379)
  2. vue 引入通用 css
  3. opencv学习笔记——minMaxIdx函数的含义及用法
  4. ES6基础教程(整理自阮一峰)
  5. Python开发【笔记】:接口
  6. Day07 jdk5.0新特性&Junit&反射
  7. api收录
  8. SpringBoot开启缓存注解
  9. Spark的Java API例子详解
  10. Miller-Rabin素数测试算法(POJ1811Prime Test)