【问题标题】:Aggregate total sum for like entries in MongoDb using Aggregration framework使用聚合框架聚合 MongoDb 中类似条目的总和
【发布时间】:2015-06-09 14:42:38
【问题描述】:

我有一个庞大的集合只包含这种文档。

  {
        "_id" : "https://example.com/test.html",
        "Count" : 1503.0000000000000000
    }, 
    {
        "_id" : "http://example.org/gr/",
        "Count" : 715.0000000000000000
    }, 
    {
        "_id" : "https://example.com/document/d//edit",
        "Count" : 710.0000000000000000
    }, 
    {
        "_id" : "http://example.org/gr/test.htm",
        "Count" : 429.0000000000000000
    }
}

如何使用 mongodb 聚合框架来实现这个结果。

 {
        "_id" : "https://example.com/",
        "Count" : 2213.0000000000000000
    }, 
    {
        "_id" : "http://example.org/",
        "Count" : 1144.0000000000000000
    }
}

在 $project 管道中拆分后具体如何使用文本搜索?

提前致谢!!

【问题讨论】:

    标签: mongodb mongoose mongodb-query


    【解决方案1】:

    首先,您必须使用 $substr 仅检索每个 URI 的开头。

    那么你应该可以$group$sum搞定了。

    第一部分可能会变得棘手和/或不可能,因为我不知道有任何运算符返回 第三个​​斜杠的位置 在字符串中。


    虽然我的建议是重写代码,以便在插入之前拆分字符串。即

    {
      id: ObjectId("..."),
      domain: "http://example.com",
      path: "test.html",
      count: 1503
    }
    

    当子域也应该可以访问时,我会去或类似的东西:

    {
      id: ObjectId("..."),
      uri: "http://sub.example.org/foo.html",
      protocol: "http",
      subdomain: "sub",
      domain: "example.org",
      path: "foo.html",
      count: 1503
    }
    

    这当然在插入时可能会慢一些,但是你可以查询很多东西。

    【讨论】:

      猜你喜欢
      • 2013-07-20
      • 2013-03-02
      • 1970-01-01
      • 2021-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-08
      • 1970-01-01
      相关资源
      最近更新 更多