【发布时间】:2021-01-03 14:57:54
【问题描述】:
我的 用户 集合包含这样的数据
{
"_id" : ObjectId("5da594c15324fec81d000027"),
"password" : "******",
"activation" : "Active",
"userType" : "Author",
"email" : "something@gmail.com",
"name" : "Something",
"profilePicture" : "profile_pictures/5da594c15324fec81d0000271607094354423image.png",
"__v" : 0
}
另一方面 userlogs 有这样的数据
{
"_id" : ObjectId("5fcb7bb4485c34a41900002b"),
"duration" : 2.54,
"page" : 1,
"activityDetails" : "Viewed Page for seconds",
"contentType" : "article",
"activityType" : "articlePageStayTime",
"label" : 3,
"bookId" : ObjectId("5f93e2cc74153f8c1800003f"),
"ipAddress" : "::1",
"creator" : ObjectId("5da594c15324fec81d000027"),
"created" : ISODate("2020-12-05T12:23:16.867Z"),
"__v" : 0
}
我正在尝试做的相当于这个 sql 查询
SELECT name,count(page),sum(duration),avg(DISTINCT(label)),COUNT(DISTINCT(bookId)) FROM users JOIN userlogs ON users._id=userlogs.creator where userlogs.activityType<>"articleListeningTime" group by users._id.
我可以一起做正常的group by 和sum。但是如何做avg distinct 和count distinct 呢?我正在使用 mongodb 3.2 版
【问题讨论】:
-
您是否将密码存储为明文?你不应该这样做,而是存储一个哈希值。
-
感谢朋友的提问!我将其存储为哈希值。粘贴到这里后就用星号替换了。
标签: node.js mongodb nosql aggregation-framework