[英]rmongodb not returning distinct values for keys
I am using R to access a MongoDb table which contain records from Google Directions API. 我使用R来访问包含来自Google Directions API的记录的MongoDb表。 While I am able to access the
_id
values, I receive an error when trying to access a different key in the database data
, - which is an array containing all of the information about routes. 虽然我能够访问
_id
值,但在尝试访问数据库data
的其他键时收到错误 - 这是一个包含有关路由的所有信息的数组。 Any idea how to query an array using rmongodb
? 知道如何使用
rmongodb
查询数组吗?
In the code below, first I check how many records are in the collection. 在下面的代码中,首先我检查集合中有多少条记录。 Then using
mongo.distinct()
I query the database for all of the object ids, and then I run into a problem when I try to access the next index - data. 然后使用
mongo.distinct()
我在数据库中查询所有对象id,然后当我尝试访问下一个索引时遇到问题 - 数据。 Any idea why this is happening? 知道为什么会这样吗? I have included an image with information about both indices from MongoDB compass.
我已经包含了一个图像,其中包含有关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")
You should be able to query it exactly as you are trying. 您应该能够在尝试时完全查询它。
In the absence of your data, here's a working example 在没有您的数据的情况下,这是一个有效的例子
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))
In a mongodb client (I'm using Robomongo) we can see data, and that image_url
is an array 在mongodb客户端(我使用的是Robomongo),我们可以看到数据,而
image_url
是一个数组
So your query should work 所以你的查询应该有效
## 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"
We can insert more data and run the same query 我们可以插入更多数据并运行相同的查询
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"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.