【发布时间】:2012-08-17 09:42:31
【问题描述】:
我正在尝试使用rmongodb 从 MongoDB 数据库中获取信息,以便在 R 中进行进一步处理。但是,我很难真正开始。这个有效:
cursor <- mongo.find(mongo, "people", query=list(last.name="Smith", first.name="John"),
fields=list(address=1L, age=1L))
while (mongo.cursor.next(cursor)){
print(mongo.cursor.value(cursor))}
现在,如果我想查找名字是“John”、“Bob”或“Catherine”的人怎么办?我试过query=list(last.name="Smith", first.name=c(John, Bob, Catherine)) 但这没有成功。将= 替换为% 也不起作用。
另一个问题是数据库内容是嵌套的,这意味着我有子树、子子树等。例如,对于条目first.name="John", last.name="Smith",我可能有像address, age, occupation 这样的子条目,而对于职业,我可能有类别作为子树(例如,从 2005 年到 2012 年,每年我都会有一个条目,如“失业”、“文员”、“企业家”)。那么,如果我想查找所有 40 岁且在 2010 年失业的名字为“John”的人怎么办?查询会是什么样子?
编辑作为对 Stennie 的回复:这是我的数据库结构和我正在尝试执行的查询的示例。想象一所大学的校友被细分为小组(例如“非常优秀的学生”、“优秀的学生”等)。然后,每个组都包含已分配到该组的人员列表及其详细信息。
(0){..}
_id : (Object ID) class id
groupname: (string) unique name for this group (e.g. "beststudents")
members[11]
(0){..}
persid : (integer) 1
firstname: (string)
surname: (string)
age: (integer)
occupation: (string)
(1){..}
persid : (integer) 2
firstname: (string)
surname: (string)
age: (integer)
occupation: (string)
# and so on until (10){..}
(1){..}
_id : (Object ID) class id
groupname: (string) unique name for this group
members[3]
(0){..}
persid : (integer) 1
firstname: (string)
surname: (string)
age: (integer)
occupation: (string)
# and so on until (2){..}
# and many more
现在假设我对名称为“最佳学生”和“好学生”的组感兴趣,并希望将每个组的每个成员的“姓氏”和“职业”作为 R 对象为了做一些情节,统计数据或其他什么。也许我还想改进这个请求,只接收那些年龄小于 40 岁的成员。现在在阅读了 Stennie 的回复后,我尝试了这种方式:
cursor <- mongo.find(mongo, "test.people",
list(groupname=list('$in'=c("beststudents", "goodstudents")),
members.age=list('$lt'=40) # I haven't tried this with my DB, so I hope this line is right
),
fields=list(members.surname=1L, members.occupation=1L)
)
count <- mongo.count(mongo, "test.people",
list(groupname=list('$in'=c("beststudents", "goodstudents")),
members.age=list('$lt'=40)
)
)
surnames <- vector("character", count)
occupations <- vector("character", count)
i <- 1
while (mongo.cursor.next(cursor)) {
b <- mongo.cursor.value(cursor)
surnames[i] <- mongo.bson.value(b, "members.surname")
occupations[i] <- mongo.bson.value(b, "members.occupation")
i <- i + 1
}
df <- as.data.frame(list(surnames=surnames, occupations=occupations))
运行后没有错误消息,但我得到一个空数据框。这段代码有什么问题?
【问题讨论】:
-
你能发布一个示例文档吗?理想情况下是一个简化的示例,显示您的查询的典型嵌套。