2017-10-07 63 views
2

我有我的MongoDB集合這樣一個JSON文件: 更新文件:Mongolite GROUP BY/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 
+0

我跑你的過採樣JSON文檔彙總查詢,並得到'{ 「_id」: 「問候語」, 「伯爵」:1.0}'請問是什麼問題? –

+0

但我沒有得到這個輸出。正如我前面所述,我正在使用Rstudio,而且我仍然陷於這個問題。你能告訴我你在哪裏試過這段代碼嗎? – nitishkrs

+0

我剛剛嘗試了robomongo的代碼 –

回答

5

如有合作意向屬性類型爲字符串,並保留對象的字符串。 我們可以將它拆分爲數組\"並使用第三項數組。

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

結果:

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

非常感謝好友....代碼就像一個魅力。 – nitishkrs