Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JSON对象上的Mongorite group by/aggregate_Json_Mongodb_Group By_Aggregate_Mongolite - Fatal编程技术网

JSON对象上的Mongorite group by/aggregate

JSON对象上的Mongorite group by/aggregate,json,mongodb,group-by,aggregate,mongolite,Json,Mongodb,Group By,Aggregate,Mongolite,在我的mongodb集合中有这样一个json文档: 更新文件: { "_id" : ObjectId("59da4aef8c5d757027a5a614"), "input" : "hi", "output" : "Hi. How can I help you?", "intent" : "[{\"intent\":\"greeting\",\"confidence\":0.8154089450836182}]", "entities" : "[]", "context" : "{\"conve

在我的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字段上执行group by,我正在蒙古人图书馆使用Rstudio。 我尝试的是:

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

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

print(stats)

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

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

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

我在示例json文档上运行聚合查询,得到
{u id:“问候语”,“计数”:1.0}
问题出在哪里?但我没有得到这个输出。正如我前面所说,我使用的是Rstudio,但我仍然无法解决这个问题。你能告诉我你在哪里尝试过这个代码吗?我刚刚用Robomonogo试过你的代码。谢谢你迄今为止的帮助,你是对的。这个查询正在处理我提供的文档,但这只是真正的问题的子集文档。我已经更新了问题中的真实文档…聚合不起作用…问题是,intent属性值不是string object,您需要将该行转换为
“intent”:[{“intent”:“greeting”,“confidence”:0.8154089450836182}],
{
    "_id" : "greeting",
    "count" : 1.0
}