【问题标题】:Counting words in the array - How can I query with mongo?计算数组中的单词 - 如何使用 mongo 进行查询?
【发布时间】:2020-11-25 06:29:17
【问题描述】:
"sourceList": [
        {
           "source" : "hello world, how are you?",
           "_id" : ObjectId("5f0eb9946db57c0007841153")
        },
        {
           "source" : "hello world, I am fine",
           "_id" : ObjectId("5f0eb9946db57c0007841153")
        },
        {
           "source" : "Is it raining?",
           "_id" : ObjectId("5f0eb9946db57c0007841153")
        }
]

hello world, how are you? = 5,hello world, I am fine = 5,Is it raining?= 3 中的总字数。

因此总字数 = 13

是否有一个 mongo 查询来做这个计算?我可以使用 javascript 来做到这一点,但有没有直接通过 mongo 查询的方法?

编辑


有没有办法可以跨文档进行此查询?对于符合特定标准的文档,我想运行一个类似的计算并增加一个约束,即重复句子的单词不会被计算两次。例如,

文档 - 1

"sourceList": [
    {
       "source" : "hello world, how are you?",
       "_id" : ObjectId("5f0eb9946db57c0007841153")
    },
    {
       "source" : "hello world, I am fine",
       "_id" : ObjectId("5f0eb9946db57c0007841153")
    },
    {
       "source" : "Is it raining?",
       "_id" : ObjectId("5f0eb9946db57c0007841153")
    }
]

文档 - 2

  "sourceList": [
    {
       "source" : "hello world, how are you?",
       "_id" : ObjectId("5f0eb9946db57c0007841153")
    },
    {
       "source" : "hello world, I am fine",
       "_id" : ObjectId("5f0eb9946db57c0007841153")
    },
    {
       "source" : "Is it raining?",
       "_id" : ObjectId("5f0eb9946db57c0007841153")
    }
]

这里的计数仍然保持不变。原因是,两个文档中的句子完全相同。但是如果我们结合 Document 1 + Document 3(如下给出)

"sourceList": [
    {
       "source" : "Look at the beautiful tiger!",
       "_id" : ObjectId("5f0eb9946db57c0007841153")
    }
]

计数为 13 + 5(文档 3)= 18。

【问题讨论】:

    标签: database mongodb mongoose mongodb-query nosql


    【解决方案1】:

    是的,您可以借助强大的聚合框架来做到这一点。

    mongo play-ground

    db.collection.aggregate([
      {
        "$unwind": "$sourceList" //For each array element
      },
      {
        $project: {
          "sp": {
            $split: [
              "$sourceList.source", //split by spaces
              " "
            ]
          }
        }
      },
      {
        "$project": {
          "sizes": {
            "$size": "$sp". //count the words in each array
          }
        }
      },
      {
        "$group": {
          "_id": "$_id",
          "count": {
            "$sum": "$sizes" //group by id to reverse unwind and add the sizes
          }
        }
      }
    ])
    

    更新:

    play

    db.collection.aggregate([
      {
        "$unwind": "$sourceList"
      },
      {
        $project: {
          "sp": {
            $split: [
              "$sourceList.source",
              " "
            ]
          }
        }
      },
      {
        "$project": {
          "sizes": {
            "$size": "$sp"
          }
        }
      },
      {
        "$group": {
          "_id": null,
          "count": {
            "$sum": "$sizes"
          }
        }
      }
    ])
    

    对于大型集合,您可能需要使用allowDiskUse,但对于大型集合而言,这是非常繁重的操作。

    更新:

    play

    db.collection.aggregate([
      {
        "$unwind": "$sourceList"
      },
      {
        $project: {
          "sp": {
            $split: [
              "$sourceList.source",
              " "
            ]
          }
        }
      },
      {
        "$group": {
          "_id": null,
          "elements": {
            $addToSet: "$sp"
          }
        }
      },
      {
        "$unwind": "$elements"
      },
      {
        "$project": {
          "sizes": {
            "$size": "$elements"
          }
        }
      },
      {
        "$group": {
          "_id": null,
          "count": {
            "$sum": "$sizes"
          }
        }
      }
    ])
    

    【讨论】:

    • 有没有办法在整个文档集合中做到这一点?我创建了一个编辑。
    • 我会更新答案。你应该在第一时间提到:)
    • 感谢您的建议!多亏了你,今天实际上学到了一些新东西。在我看来,这是 mongodb 和其他 db 在支持服务器端统计分析方面真正胜过 firebase 的时候。
    • 更新后的查询还将考虑重复字符串的字数。例如,mongoplayground.net/p/up4wukJWCjc 将输出总字数为21,但它必须是18,因为Is it raining? 在两个文档中都重复了。
    • 好的,必须删除重复项吗? - 重复的单词或重复的语句?
    猜你喜欢
    • 1970-01-01
    • 2021-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-18
    相关资源
    最近更新 更多