下面是我的架构..我需要使用不同的'user_email'来获取我的'file_id'(我将定义我的file_id)的计数..注意......不是具有相似和不同user_email的file_id的计数我只需要的计数我的 file_id 与不同的 user_email.any 有帮助吗?
{
"_id" : ObjectId("591bedc8c942a3514ed09b24"),
"file_id" : "58450c20b053dc426b935927",
"description" : "get your output ready",
"user_email" : "[email protected]",
"__v" : 0
}
{
"_id" : ObjectId("591bedc8c942a3514ed09b25"),
"description" : "get your output ready",
"user_email" : "[email protected]",
"__v" : 0
}
{
"_id" : ObjectId("591bedc8c942a3514ed09b26"),
"file_id" : "58450c20b053dc426b935927",
"description" : "get your output ready",
"user_email" : "[email protected]",
"__v" : 0
}
{
"_id" : ObjectId("591bedc8c942a3514ed09b27"),
"file_id" : "58450c20b053dc426b935927",
"description" : "get your output ready",
"user_email" : "[email protected]",
"__v" : 0
}
{
"_id" : ObjectId("591beea4a62559b4506549e0"),
"file_id" : "58450c20b053dc426b935927",
"description" : "get your output ready",
"user_email" : "[email protected]",
"__v" : 0
}
db.myschema.aggregate({$group:{"file_id":"58450c20b053dc426b935927","user_email" : "[email protected]",count: { $sum: 1 }} })
以下查询应该为您提供file_id
和的唯一计数user_email
db.myschema.aggregate([
{
$group: {
_id: {"file_id": "$file_id", "user_email": "$user_email"},
count: {$sum: 1}
}
},
{
$project: {
_id: 0,
"user_email": "$_id.user_email",
"file_id": "$_id.file_id",
"count": 1
}
}
]);
编辑因此,如果您只对特别感兴趣,file_id
那么您可以简单地按如下方式过滤结果:
let fileId = '58450c20b053dc426b935927';
db.myschema.aggregate([
{
$group: {
_id: { file_id: "$file_id", user_email: "$user_email" },
count: { $sum: 1 }
}
},
{
$match: {
"_id.file_id": fileId
}
},
{
$project: {
_id: 0,
user_email: "$_id.user_email",
file_id: "$_id.file_id",
count: 1
}
}
]);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句