Queries in MongoDB

前端 未结 2 640
被撕碎了的回忆
被撕碎了的回忆 2021-02-09 10:33

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

2条回答
  •  你的背包
    2021-02-09 11:10

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

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

    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.

    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?

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

提交回复
热议问题