rmongodb: using $or in query

后端 未结 4 1586
离开以前
离开以前 2020-12-21 17:40

I\'m struggling to create a query using $or within R and rmongodb. What I\'d like to emulate is this from cmdline mongo:

db.people.find( { $or : [ {\"person         


        
相关标签:
4条回答
  • 2020-12-21 17:51

    I found this query easier to wrangle in RMongo:

    mongo <- mongoDbConnect(dbName="work", host="localhost",port='27017')
    result <- dbGetQuery(mongo, "people","
        { '$or': [
            {'person.cell':{'$exists':true}},
            {'person.home':{'$exists':true}}
        ]}"
    )
    

    Result will be a data.frame.

    0 讨论(0)
  • 2020-12-21 17:55

    To avoid having to compose the sequence of mongo.bson.buffer-statements I wrote a package (rmongodbHelper) that will translate a JSON or a list() to a BSON object which can then be used with rmongodb.

    First let's setup the environment:

    library(rmongodb)
    
    # install rmongodbHelper package from GitHub
    
    library(devtools)
    devtools::install_github("joyofdata/rmongodbHelper")
    library(rmongodbHelper)
    
    # the MongoDB instance
    
    ns <- "dbx.collx"
    M <- mongo.create()
    mongo.is.connected(M)
    mongo.remove(M, ns, json_to_bson("{}"))
    
    # inserting a number of dummy objects
    # JSON keys currently are expected to be wrapped in double quotes!
    
    objs <- c(
      '{"_id":-1}',
      '{"_id":-2, "person":{}}',
      '{"_id":-3, "person":{"x":0}}',
      '{"_id":1,  "person":{"cell":0}}',
      '{"_id":2,  "person":{"home":0}}',
      '{"_id":3,  "person":{"cell":0,"home":0}}'
    )
    
    for(obj in objs) {
      mongo.insert(M, ns, json_to_bson(obj))
    }
    

    Let's see via MongoDB shell if they were successfully inserted:

    > use dbx
    switched to db dbx
    > db.collx.find().pretty()
    { "_id" : -1 }
    { "_id" : -2, "person" : { } }
    { "_id" : -3, "person" : { "x" : 0 } }
    { "_id" : 1, "person" : { "cell" : 0 } }
    { "_id" : 2, "person" : { "home" : 0 } }
    { "_id" : 3, "person" : { "cell" : 0, "home" : 0 } }
    

    Now let's search for documents with a query:

    # searching for those objects
    # JSON keys currently are expected to be wrapped in double quotes!
    
    json_qry <- 
    '{
      "$or" : [ 
        {"person.cell": { "$exists" : true } }, 
        {"person.home": { "$exists" : true } } 
      ] 
    }'
    
    cur <- mongo.find(M, "dbx.collx", json_to_bson(json_qry))
    
    while(mongo.cursor.next(cur)) {
        print(mongo.cursor.value(cur))
    }
    

    And this is what we get in the end:

    _id : 1      1.000000
    person : 3   
        cell : 1     0.000000
    
    _id : 1      2.000000
    person : 3   
        home : 1     0.000000
    
    _id : 1      3.000000
    person : 3   
        cell : 1     0.000000
        home : 1     0.000000
    
    0 讨论(0)
  • 2020-12-21 17:57

    your way of creating an mongo bson array is wrong. You are missing the parts

    mongo.bson.buffer.start.object(buf, "0")
    ...
    mongo.bson.buffer.finish.object(buf)
    mongo.bson.buffer.start.object(buf, "1")
    ...
    mongo.bson.buffer.finish.object(buf)
    

    For a working example please check the latest comment on: https://github.com/mongosoup/rmongodb/issues/17

    I hope this works for now.

    There is a bug in all the .to.list / .from.list / .append.list commands. I am working on an easier solution!

    0 讨论(0)
  • bson <- mongo.bson.from.JSON('{ "$or" : [ {"person.cell": { "$exists" : true } }, {"person.home": { "$exists" : true } } ] }')
    mongo.find(mongo, "work.people", bson)
    
    0 讨论(0)
提交回复
热议问题