美文网首页
R连接rmongodb

R连接rmongodb

作者: 划船的浆不浪 | 来源:发表于2017-01-09 11:36 被阅读41次
    #connect
    library(rmongodb)
    mongo <- mongo.create(host = '主机',
                          username = '用户名',
                          password = '密码',
                          db = 'media')
    mongo.is.connected(mongo)
    mongo.get.database.collections(mongo,db = 'media')
    
    mongo.count(mongo,ns = 'tablename')
    #-------------------------------------------------------------------------------
    # get data
    #----------------------------------------------------------------------------------
    startDate <- as.POSIXct("2016-12-18 16:00:00",tz='GMT')
    endDate <- as.POSIXct("2016-12-25 16:00:00",tz='GMT')
    q <- mongo.bson.from.list(list("date" = list("$gte" = startDate,"$lte" = endDate)))
    list <- mongo.find.all(mongo,ns = 'tablename',q,fields = '{"wechat_id":1,"gzh_name":1,"title":1,"url":1,"read_num":1,"like_num":1,"date":1,"updated_at":1,"created_at":1}')
    #创建dataframe
    df <- data.frame( wechat_id = NA,
                      gzh_name = NA,
                      title = NA,
                      url = NA,
                      read_num = NA,
                      like_num = NA,
                      date = NA,
                      updated_at = NA,
                      created_at = NA
                      
                      
    )
    

    相关文章

      网友评论

          本文标题:R连接rmongodb

          本文链接:https://www.haomeiwen.com/subject/qrzcbttx.html