如何使用rmongodb在R中查询mongodb

时间:2015-04-18 03:49:13

标签: rmongodb

我的数据在mongodb的集合“bd”中,在数据库“mydb”中。 它的亚马逊审查数据,我关注字段“product / productId”和与之相关的数据

#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]}

1 个答案:

答案 0 :(得分:1)

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

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

mongo.find.all(mongo, coll, list("product/productId" = res[i]))
相关问题