gpt4 book ai didi

json - Mongolite 分组依据/聚合 JSON 对象

转载 作者:可可西里 更新时间:2023-11-01 09:58:39 26 4
gpt4 key购买 nike

我的 mongodb 集合中有一个这样的 json 文档:更新文档:

{
"_id" : ObjectId("59da4aef8c5d757027a5a614"),
"input" : "hi",
"output" : "Hi. How can I help you?",
"intent" : "[{\"intent\":\"greeting\",\"confidence\":0.8154089450836182}]",
"entities" : "[]",
"context" : "{\"conversation_id\":\"48181e58-dd51-405a-bb00-c875c01afa0a\",\"system\":{\"dialog_stack\":[{\"dialog_node\":\"root\"}],\"dialog_turn_counter\":1,\"dialog_request_counter\":1,\"_node_output_map\":{\"node_5_1505291032665\":[0]},\"branch_exited\":true,\"branch_exited_reason\":\"completed\"}}",
"user_id" : "50001",
"time_in" : ISODate("2017-10-08T15:57:32.000Z"),
"time_out" : ISODate("2017-10-08T15:57:35.000Z"),
"reaction" : "1"

我需要在 intent.intent 字段上执行分组,并且我将 Rstudio 与 mongolite 库一起使用。我试过的是:

pp = '[{"$unwind": "$intent"},{"$group":{"_id":"$intent.intent", "count": {"$sum":1} }}]'

stats <- chat$aggregate(
pipeline=pp,
options = '{"allowDiskUse":true}'
)

print(stats)

但是它不起作用,上面代码的输出是

  _id count
1 NA 727

最佳答案

如果 intent 属性类型为字符串,则将对象保留为字符串。我们可以使用 \" 将其拆分为数组并使用数组的第三项。

db.getCollection('test1').aggregate([
{ "$project": { intent_text : { $arrayElemAt : [ { $split: ["$intent", "\""] } ,3 ] } } },
{ "$group": {"_id": "$intent_text" , "count": {"$sum":1} }}
])

结果:

{
"_id" : "greeting",
"count" : 1.0
}

关于json - Mongolite 分组依据/聚合 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46620437/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com