获取集合mongodb中所有文档中多个元素的频率 [英] Get frequency for multiple elements in all documents inside a collection mongodb
问题描述
这就是我的问题.
我是mongodb的新手,并且有一个收集文档的集合,如下所示:
{"_id":{"$ oid":"60626db173b4ca321c02ee3e";},年份":"2021","name":"Book 1",作者":["Joe,B","Jessica,K"],"createdAt":{"$ date":"2021-03-30T00:15:45.859Z"}},{"_id":{"$ oid":"60626db173b4ca321c02ee4e";},年份":"2021",作者":[卡尔,B",杰西卡,K"],名称":"Book 2""createdAt":{"$ date":"2021-03-30T00:15:45.859Z"}},
我需要了解所有作者的频率和书籍的年代.
期望的结果将是这样的(只要我能获得每个元素的频率,返回结果的方式并不重要)
{作者":{"Joe,B":1,卡尔,B":1,"Jessica,K":2},年份":{"2021":2}}
我已经看到了该线程如何计算每个值都在数组中?在一个数组中完成工作,但是我不知道是否有可能同时获取多个元素(年份,作者)的频率,或者如何做到这一点.>
感谢您的帮助.谢谢.
演示- https://mongoplayground.net/p/95JtQEThxvV
$ group ://docs.mongodb.com/manual/reference/operator/aggregation/push/"rel =" nofollow noreferrer> $ push 作者进入数组即可得到 $展开.
由作者$ group 以null组合所有文档,请使用 $ addToSet 推送唯一值并转换 $ project
db.collection.aggregate([{$ group:{_id:{year:"$ year"},作者:{$ push:" $ authors"},yearCount:{$ sum:1}}},{$ unwind:" $ authors"},{$ unwind:" $ authors"}},{$ group:{_id:{author:" $ authors"},年:{$ first:" $ _ id.year"},yearCount:{$ first:"$ yearCount"},作者:{$ push:" $ authors"},authorCount:{$ sum:1}}},{"$ group":{_id:null,年: {$ addToSet:{k:"$ year",v:"$ yearCount";}},作者:{$ addToSet:{k:"$ _ id.author",v:"$ authorCount";}}}},{$ project:{_id:0,年:{$ arrayToObject:" $ years"},作者:{$ arrayToObject:" $ authors"}}}])
演示2-按年份分组的作者数- https://mongoplayground.net/p/_elnjmknroF
So heres my problem.
I am new to mongodb and have a collection which documents are saved like this:
{
"_id": {
"$oid": "60626db173b4ca321c02ee3e"
},
"year": "2021",
"name": "Book 1",
"authors": ["Joe, B", "Jessica, K"],
"createdAt": {
"$date": "2021-03-30T00:15:45.859Z"
}
},
{
"_id": {
"$oid": "60626db173b4ca321c02ee4e"
},
"year": "2021",
"authors": ["Carl, B", "Jessica, K"],
"name": "Book 2"
"createdAt": {
"$date": "2021-03-30T00:15:45.859Z"
}
},
I need to get both the frequency of all authors and the years of the books.
The expected result would be something like this (as long as i can get each element frequency it doesn't really matter how the results are returned):
{
"authors": {
"Joe, B": 1,
"Carl, B": 1,
"Jessica, K": 2
},
"year": {
"2021": 2
}
}
I've seen this thread How to count occurence of each value in array? which does the job in one array but i have no idea if its possible to adapt to get the frequency of multiple elements (year, authors) at the same time or how to do it.
I appreciate any help. Thank you.
Demo - https://mongoplayground.net/p/95JtQEThxvV
$group by year $push authors into the array get $sum count of the year occurrence, $unwind into individuals documents.
$group by authors and get $sum count of the author occurrence
$group by null to combine all documents, use $addToSet to push unique values and convert $arrayToObject to get final output in $project
db.collection.aggregate([
{
$group: {
_id: { year: "$year" },
authors: { $push: "$authors" },
yearCount: { $sum: 1 }
}
},
{ $unwind: "$authors" },
{ $unwind: "$authors"},
{
$group: {
_id: { author: "$authors" },
year: { $first: "$_id.year" },
yearCount: { $first: "$yearCount" },
authors: { $push: "$authors" },
authorCount: { $sum: 1 }
}
},
{
"$group": {
_id: null,
years: {
$addToSet: { k: "$year", v: "$yearCount" }
},
authors: {
$addToSet: { k: "$_id.author", v: "$authorCount" }
}
}
},
{
$project: {
_id: 0,
years: { $arrayToObject: "$years" },
authors: { $arrayToObject: "$authors" }
}
}
])
Demo 2 - For author count grouped by year- https://mongoplayground.net/p/_elnjmknroF
这篇关于获取集合mongodb中所有文档中多个元素的频率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!