错误:无效的JSON对象

问题描述 投票:2回答:2

我试图运行此查询并继续出现此错误:

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 }}}]
json r mongodb
2个回答
3
投票

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
投票

尝试添加:

js <- '[{"$unwind":"$cdr"}, {"$lookup":{"from": "inventory", localField:}]'
© www.soinside.com 2019 - 2024. All rights reserved.