Search code examples
rrmongodb

rmongodb $in query issues


I have really enjoyed working with rmongodb and have figured out how to do a lot of different things/combination of queries but this one still stumps me. I am trying to get all of the documents that have the symbol: field equal to an element of a vector (named symbols in r).

Query that works on my MongoDB DB that I am trying to replicate in r

{symbol: {"$in": ["SYMBOLA","SYMBOLB","SYMBOLC","SYMBOLD","SYMBOLE"]} }

In r...

symbols <- list("SYMBOLA","SYMBOLB","SYMBOLC","SYMBOLD","SYMBOLE")
mongo.find.all(mongo, namespace, query = list(symbol = list('$in' = symbols)))

Doesnt seem to work (since I am not used to the syntax I tried all sorts of combinations without luck. Feel like I am missing something here.

Any help on how I can query specific documents based on a symbols vector in R would be a huge help.

EDIT: Here is the print of the BSON:

symbol : 3   
        $in : 4      
            0 : 2    SYMBOLA
            1 : 2    SYMBOLB
            2 : 2    SYMBOLC
            3 : 2    SYMBOLD
            4 : 2    SYMBOLE

Solution

  • This works fine on my machine

    symbols <- list("SYMBOLA","SYMBOLB","SYMBOLC","SYMBOLD","SYMBOLE")
    mongo <- mongo.create()
    for (ch in symbols) 
      mongo.insert(mongo, 'test.test', list(symbol = ch))
    # insert one more symbol that don't match our condition
    mongo.insert(mongo, 'test.test', list(symbol = 'incorrect_symbol'))
    
    res <- mongo.find.all(mongo, 'test.test', query = list(symbol = list('$in' = symbols)))
    length(res)
    #> 5