2017-02-20 97 views
2

我試圖運行此查詢,並保持有此錯誤:錯誤:無效JSON對象

install.packages(「mongolite」) 
library(mongolite) 

m <- mongo(db = "ionmom") 
m6 <- m$aggregate('[{"$unwind":"$cdr"}, {$lookup:{from: "inventory", localField: "_id", foreignField: "_id", as:"inventory"}},{$unwind: "$inventory"}, {"$project":{ "$project": {"cdr.duration": 1, "inventory.wearables.type":1, "inventory.wearables.status":1, "inventory.wearables.battery":1 }}}]') 

# Error: Invalid JSON object: [{"$unwind":"$cdr"}, {$lookup:{from: "inventory", localField: "_id", foreignField: "_id", as:"inventory"}},{$unwind: "$inventory"}, {"$project":{ "$project": {"cdr.duration": 1, "inventory.wearables.type":1, "inventory.wearables.status":1, "inventory.wearables.battery":1 }}}] 

回答

2

mongolite使用jsonlite引擎蓋下做的JSON解析。如果你把你的查詢通過jsonlite::fromJSON()你會看到這個問題

js <- '[{"$unwind":"$cdr"}, {$lookup:{from: "inventory", localField: "_id", foreignField: "_id", as:"inventory"}},{$unwind: "$inventory"}, {"$project":{ "$project": {"cdr.duration": 1, "inventory.wearables.type":1, "inventory.wearables.status":1, "inventory.wearables.battery":1 }}}]' 

jsonlite::fromJSON(js) 

# Error: lexical error: invalid char in json text. 
#     [{"$unwind":"$cdr"}, {$lookup:{from: "inventory", loc 
#      (right here) ------^ 

這是告訴你的JSON結構是無效的,因爲它的預期報價" "圍繞每個字符串

js <- '[{"$unwind":"$cdr"}, {"$lookup":{"from": "inventory", "localField": "_id", "foreignField": "_id", "as":"inventory"}},{"$unwind": "$inventory"}, {"$project":{ "$project": {"cdr.duration": 1, "inventory.wearables.type":1, "inventory.wearables.status":1, "inventory.wearables.battery":1 }}}]' 

m$aggregate(js) 

## I don't have your data ... 

# Imported 0 records. Simplifying into dataframe... 
# data frame with 0 columns and 0 rows 
+0

非常感謝它的工作完善 –

+0

@HueyDucVu - 不客氣。可以通過按下投票箭頭下的'嘀嗒'來「接受」答案:) – SymbolixAU

+0

我得到要導入數據框的數據,但一個問題是可穿戴設備在庫存集合數組中,我該如何解開可穿戴設備陣列。 –