如何在mongodb中按类别总结标签

  • 本文关键字:标签 mongodb mongodb
  • 更新时间 :
  • 英文 :


我有一个形状像这样的集合:

[
{
_id: ObjectId("5d8e8c9b8f8b9b7b7a8b4567"),
tags: {
language: [ 'en' ],
industries: [ 'agency', 'travel' ],
countries: [ 'ca', 'us' ],
regions: [ 'north-america' ],
}
},
{
_id: ObjectId("5d8e8c9b8f8b9b7b7a8b4568"),
tags: {
language: [ 'en', 'fr' ],
industries: [ 'travel' ],
countries: [ 'ca' ]
}
},
{
_id: ObjectId("5d8e8c9b8f8b9b7b7a8b4569"),
tags: {
language: [ 'en' ],
industries: [ 'agency', 'travel' ],
countries: [ 'ca', 'us' ],
regions: [ 'south-america' ]
}
},
]

,我想生成这个结果…

{
//* count of all documents
"count": 3,
//* count of all documents that contain any slug within the given category
"countWithCategorySlug": {
"language": 3,
"industries": 3,
"countries": 3,
"regions": 2
},
//* per category: count of documents that contain that slug in the givin category
"language" {
"en": 3,
"fr": 1
},
"industries" {
"agency": 2,
"travel": 3,
},
"countries" {
"ca": 3,
"us": 2
},
"regions" {
"north-america": 1,
"south-america": 1
}
}

超级卡住了,所以任何帮助都会很感激。:)

类别的数量是未知的,我有一个代码解决方案,查询不同类别和蛞蝓的列表,然后为每个生成$group stage…结果查询太大了,需要更好的方法…问题是我完全不知道如何优化它…

查询
  • 在facet完成之前的第一部分,将它们分开,并为每个值1的文档创建
[{
"type": "language",
"value": "en",
"_id": ObjectId("5d8e8c9b8f8b9b7b7a8b4567")
},
{
"type": "industries",
"value": "agency",
"_id": ObjectId("5d8e8c9b8f8b9b7b7a8b4567")
},
{
"type": "industries",
"value": "travel",
"_id": ObjectId("5d8e8c9b8f8b9b7b7a8b4567")
},
{
"type": "countries",
"value": "ca",
"_id": ObjectId("5d8e8c9b8f8b9b7b7a8b4567")
}]
  • ,然后facet与3个字段和计数文档
  • 转换之后,键上的数据像预期的输出

Playmongo

ggregate(
[{"$set": {"tags": {"$objectToArray": "$tags"}}},
{"$set": 
{"tags": 
{"$map": 
{"input": "$tags",
"in": {"type": "$$this.k", "value": "$$this.v", "_id": "$_id"}}}}},
{"$unwind": "$tags"},
{"$replaceRoot": {"newRoot": "$tags"}},
{"$unwind": "$value"},
{"$facet": 
{"count": 
[{"$group": {"_id": null, "count": {"$addToSet": "$_id"}}},
{"$set": {"count": {"$size": "$count"}}}],
"category": 
[{"$group": {"_id": "$type", "count": {"$addToSet": "$_id"}}},
{"$set": {"count": {"$size": "$count"}}}],
"values": 
[{"$group": 
{"_id": "$value",
"type": {"$first": "$type"},
"values": {"$addToSet": "$_id"}}},
{"$set": {"values": {"$size": "$values"}}},
{"$group": 
{"_id": "$type",
"values": 
{"$push": 
{"type": "$type", "value": "$_id", "count": "$values"}}}}]}},
{"$set": 
{"count": 
{"$getField": 
{"field": "count", "input": {"$arrayElemAt": ["$count", 0]}}},
"category": 
{"$arrayToObject": 
[{"$map": 
{"input": "$category",
"in": {"k": "$$this._id", "v": "$$this.count"}}}]},
"values": 
{"$arrayToObject": 
[{"$map": 
{"input": "$values",
"in": 
{"k": "$$this._id",
"v": 
{"$arrayToObject": 
[{"$map": 
{"input": "$$this.values",
"in": {"k": "$$this.value", "v": "$$this.count"}}}]}}}}]}}}])

输出
[{
"count": 3,
"category": {
"countries": 3,
"industries": 3,
"regions": 2,
"language": 3
},
"values": {
"regions": {
"south-america": 1,
"north-america": 1
},
"countries": {
"us": 2,
"ca": 3
},
"language": {
"fr": 1,
"en": 3
},
"industries": {
"agency": 2,
"travel": 3
}
}
}]

最新更新