1

我正在使用 mongolite 包从 MongoDB 连接和检索数据,请帮助我从当前日期和时间检索最后一天的数据。

##connecting mongodb

library(mongolite)

mongo<-mongolite::mongo(collection = "Sample", db = "Test", url = 
                          "mongodb://User:123@Wyyuyu:13333/ty2_U",verbose = TRUE)

## getting  data from collection

values <- mongo$find()

上述代码步骤适用于从样本集合中获取所有数据。但我需要从今天的数据和时间中只获取前一天的数据。

示例数据框。

Get <- data.frame(id = c(1,2,3,4,5,6),
                 firstName=c("kannan","anderson","jimmy","aray","stepen","james"),
                 dates =as.POSIXct(c("2017-10-25 15:10:59","2017-10-25 11:30:59 ","2017-10-24 15:30:59 ","2017-10-23 15:32:33","2017-10-24 11:22:34","2017-10-23 14:25:17")))

输出数据框

id   name      dte 
3   jimmy     2017-10-24 15:30:59

5   stepen     2017-10-24 11:22:34
4

1 回答 1

1

您可以像这样过滤数据

Get <- data.frame(id = c(1,2,3,4,5,6),
                  firstName=c("kannan","anderson","jimmy","aray","stepen","james"),
                  dates =as.POSIXct(c("2017-10-25 15:10:59","2017-10-25 11:30:59 ","2017-10-24 15:30:59 ","2017-10-23 15:32:33","2017-10-24 11:22:34","2017-10-23 14:25:17")))


Get[Get$dates > (as.Date(Sys.time()) - 1) & Get$dates < as.Date(Sys.time()),]

  id firstName               dates
3  3     jimmy 2017-10-24 15:30:59
5  5    stepen 2017-10-24 11:22:34
于 2017-10-25T05:06:13.983 回答