2017-02-20 187 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

我得到要导入数据框的数据,但一个问题是可穿戴设备在库存集合数组中,我该如何解开可穿戴设备阵列。 –