我正在使用R访问一个包含来自Google Directions API的记录的MongoDb表。虽然我能够访问_id值,但在尝试访问数据库data中的另一个键时,我收到了一个错误--它是一个包含所有路由信息的数组。您知道如何使用rmongodb查询数组吗
在下面的代码中,我首先检查集合中有多少条记录。然后使用mongo.distinct()查询数据库中的所有对象ids,然后在尝试访问下一个索引数据时遇到问题。你知道为什么会发生这种事吗?我已经包含了一幅图像,其中包含了来自MongoDB指南针的两个索引的信息。

> if(mongo.is.connected(mongo) == TRUE) {
+ help("mongo.count")
+ mongo.count(mongo, coll)
+ }
[1] 106500
> res <- mongo.distinct(mongo, coll, "_id")
> head(res)
$`0`
{ $oid : "57583d1057aa3d0499a85aab" }
$`1`
{ $oid : "57583d1157aa3d0499a85aad" }
$`2`
{ $oid : "57583d1257aa3d0499a85aaf" }
$`3`
{ $oid : "57583d1357aa3d0499a85ab1" }
$`4`
{ $oid : "57583d1457aa3d0499a85ab3" }
$`5`
{ $oid : "57583d1557aa3d0499a85ab5" }
> res <- mongo.distinct(mongo, coll, "data.legs")
Warning message:
In mongo.distinct(mongo, coll, "data.legs")发布于 2016-07-16 17:17:42
您应该能够按照您正在尝试的方式来查询它。
在没有数据的情况下,下面是一个有效的示例
library(rmongodb)
mongo <- mongo.create(db = "test")
## create some data
lst <- list(lat = -37.9,
lon = 144.5,
image_url = letters)
## insert data
mongo.insert(mongo, "test.array_test", mongo.bson.from.list(lst))在mongodb客户端(我使用的是Robomongo)中,我们可以看到数据,image_url是一个数组

因此您的查询应该是有效的
## query data on the 'image_url' array
mongo.distinct(mongo, "test.array_test", key = "image_url")
# [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z"我们可以插入更多的数据并运行相同的查询
lst <- list(lat = -37.8,
lon = 144.4,
image_url = c("string1","string2"))
mongo.insert(mongo, "test.array_test", mongo.bson.from.list(lst))
mongo.distinct(mongo, "test.array_test", key = "image_url")
# [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l"
# [13] "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" "w" "x"
# [25] "y" "z" "string1" "string2"https://stackoverflow.com/questions/38351176
复制相似问题