MongoDB中的查询 [英] Queries in MongoDB

查看:174
本文介绍了MongoDB中的查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用rmongodb从MongoDB数据库中获取信息以在R中进行进一步处理.但是,要真正开始我有些困难.这一项有效:

I'm trying to use rmongodb to fetch information from a MongoDB database for further processing in R. However, I have some difficulties to really get started. This one works:

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))}

现在,如果我想找到名字叫约翰"或鲍勃"或凯瑟琳"的人怎么办?我尝试了query=list(last.name="Smith", first.name=c(John, Bob, Catherine)),但是没有成功.将=替换为%也不起作用.

Now, what if I want to find people whose first name is either "John" or "Bob" or "Catherine"? I tried query=list(last.name="Smith", first.name=c(John, Bob, Catherine)) but this didn't work out. Replacing = with % didn't work either.

另一个问题是数据库内容是嵌套的,这意味着我有子树,子子树等.例如,对于条目first.name="John", last.name="Smith",我可能具有诸如address, age, occupation的子条目,对于职业,我可能又具有类别作为子树. (例如,从2005年到2012年,每年我都会有一个条目,例如失业",文员",企业家").那么,如果我想找到所有名字叫"John"的人,这些人今年40岁,在2010年失业,该怎么办?查询是什么样的?

Another issue is that the database content is nested, which means I have subtrees, subsubtrees etc. For example, for the entry first.name="John", last.name="Smith" I might have subentries like address, age, occupation, and for occupation again I might have categories as subtrees (e.g. years from 2005 to 2012 and for each year I would have an an entry like "unemployed", "clerk", "entrepreneur"). So what if I want to find all people with first name "John" who are 40 years old and were unemployed in 2010? What would the query look like?

编辑以回复Stennie:这是我的数据库结构和我要执行的查询的示例.想象一下,大学的校友被分为几类(例如非常好学生",好学生"等).然后,每个组都包含一个已分配给该组的人员及其详细信息的列表.

EDIT as a reply to Stennie: Here's an example of the structure of my database and the query I'm trying to do. Imagine that alumnis of a university have been subdivided into groups (e.g. "very good students", "good students" and so on). Each group then contains a list of people that have been assigned to this group along with their details.

(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的回复之后,我就这样尝试了:

Now let's assume that I am interested in the groups with the names "best students" and "good students", and would like to get "surname" and "occupation" for each member of each of these groups as an R object in order to do some plots, stats or whatever. And maybe I'd also want to refine this request to only get those members that are younger than 40 years old. Now after having read Stennie's reply, I tried it this way:

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))

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

There's no error message after running this, but I get an empty data frame. What's wrong with this code?

推荐答案

现在,如果我想找到名字叫约翰"的人怎么办 还是鲍勃"或凯瑟琳"?

Now, what if I want to find people whose first name is either "John" or "Bob" or "Catherine"?

您可以使用 $in运算符这个:

You can use the $in operator for this:

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

值得阅读一下MongoDB 高级查询页面,以及点表示法(到达对象)

It would be worth having a read of the MongoDB Advanced Queries page as well as Dot Notation (Reaching Into Objects).

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

Another issue is that the database content is nested, which means I have subtrees, subsubtrees etc.

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

The data structure sounds potentially challenging to manipulate; would need a practical example of a document to try to illustrate the query.

那么,如果我想找到所有姓"John"的人怎么办? 年龄40岁,2010年失业?查询是什么样的?

So what if I want to find all people with first name "John" who are 40 years old and were unemployed in 2010? What would the query look like?

对数据结构进行一些假设,这是一个简单的"and"查询的示例:

Making some assumptions on the data structure, here is an example of a simple "and" query:

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

这篇关于MongoDB中的查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆