-1

我在数据库“mydb”中的集合“bd”中有我的数据。它的亚马逊评论数据,我关心“产品/产品ID”字段和与之相关的数据

#loading rmongodb library
library(rmongodb)
mongo <- mongo.create()
if(mongo.is.connected(mongo) == TRUE) 
{
#selecting mydb database
db <- "mydb"
mongo.get.database.collections(mongo, db)
} 
#selecting bd collection in mydb
coll = "mydb.bd"
#putting all productId in the res variable
if(mongo.is.connected(mongo) == TRUE) 
{
#since there are duplicate values of "product/productId" field,i stored only unique values of it
res <- mongo.distinct(mongo, coll, "product/productId")
} 
#logic to get review blob for a particular productId and here is where i get error
if (mongo.is.connected(mongo) == TRUE)
{
for(i in 1:length(res))
{
#getting error here while iterating for each productId stored in res[i] 
doc <- mongo.find.all(mongo,coll,'{"product/productId": res[i]}')
}
}
#but i get error in here as==>
#Error in mongo.bson.from.JSON(arg) : 
#Not a valid JSON content: {"product/productId": res[i]}
4

1 回答 1

1

答案在错误消息中 - 您的 JSON 无效。

q <- paste('{"product/productId":', res[i], '}')
mongo.find.all(mongo, coll, q)

或者

mongo.find.all(mongo, coll, list("product/productId" = res[i]))
于 2015-04-19T08:35:49.633 回答