#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
)
网友评论