MongoDB中的查询

Anj*_*jaM 7 r mongodb rmongodb

我正在尝试使用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))}
Run Code Online (Sandbox Code Playgroud)

现在,如果我想找到名字是"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年失业了怎么办?查询会是什么样的?

编辑作为对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
Run Code Online (Sandbox Code Playgroud)

现在让我们假设我对名为"最佳学生"和"好学生"的小组感兴趣,并希望将每个小组的每个成员的"姓"和"职业"作为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))
Run Code Online (Sandbox Code Playgroud)

运行此命令后没有错误消息,但我得到一个空数据框.这段代码出了什么问题?

Ste*_*nie 3

现在,如果我想查找名字为“John”、“Bob”或“Catherine”的人,该怎么办?

您可以使用$in运算符来实现此目的:

cursor <- mongo.find(mongo, "test.people",
   list(last.name="Smith", 
        first.name=list('$in'=c('John','Bob','Catherine'))
   )
)
Run Code Online (Sandbox Code Playgroud)

值得一读 MongoDB高级查询页面以及点表示法(深入对象)

另一个问题是数据库内容是嵌套的,这意味着我有子树、子子树等。

数据结构听起来可能难以操作;需要一个文档的实际示例来尝试说明查询。

那么,如果我想查找所有名字为“John”、年龄为 40 岁且在 2010 年失业的人呢?查询会是什么样子?

对数据结构做出一些假设,下面是一个简单的“and”查询的示例:

cursor <- mongo.find(mongo, "test.people",
    list(
        first.name='John',
        fy2012.job='unemployed',
        age = 40
    )
)
Run Code Online (Sandbox Code Playgroud)