如何使用 rmongodb 在 R 中迭代 mongodb 的查询

how to iterate in a query of mongodb in R using rmongodb

我的数据在数据库 "mydb" 中的集合 "bd" 中的 mongodb 中。 它的亚马逊评论数据和我关心的字段 "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]}

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

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

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