R如何向MongoDB发出POST请求 [英] R How to make a POST request to MongoDB

查看:243
本文介绍了R如何向MongoDB发出POST请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在处理在线数据库中的数据.我通过API访问数据,该API可在一次检索所有数据时起作用.但这会使我的系统变慢,因此我只想请求过滤后的数据(直到现在我才提出).这是获取整个数据集的方法:

I'm currently working with data from an online database. I access the data via API, which works when I retrieve all data at once. But this makes my system slow, so I want to make a request only for filtered data (which I did not make until now). This is the way to get the whole dataset:

#-------------------------------#
#          packages             #
#-------------------------------#
library(httr)
library(jsonlite)

#-------------------------------#
#         API requests          #
#-------------------------------#

##  get all data at once  ##
url <- "https://www.eter-project.com/api/3.0/HEIs/full"
raw_result <- GET(url)

#-------------------------------#
#       data processing         #
#-------------------------------#

# 'status_code' (if request worked) and 'content' (APIs answer) important
names(raw_result) 

# '200' tells us that server received request
raw_result$status_code 

# translate Unicode into text
this.raw.content <- rawToChar(raw_result$content) 

# transform json into workable format for R
mydata <- fromJSON(this.raw.content, flatten = TRUE) 

class(mydata)
dim(mydata)

根据文档( https://www.eter-project.com /api/doc/#/)我需要使用url https://www.eter-project.com/api/3.0/HEIs/query和以下结构中嵌入的过滤器的POST请求:

According to the documentation (https://www.eter-project.com/api/doc/#/) I need a POST request using url https://www.eter-project.com/api/3.0/HEIs/queryand a filter embedded in the following structure:

{
 "filter": {},
  "fieldIds": {}
}

我想过滤多年和国家/地区,以便仅获取当前要使用的数据.过滤器的结构为 { "BAS.REFYEAR.v": 2011, "BAS.COUNTRY.v": "AT"}.

I want to filter for years and countries in order to only get the data I currently want to work with. The structure for the filter would be { "BAS.REFYEAR.v": 2011, "BAS.COUNTRY.v": "AT"}.

有人知道,我如何将其实现到POST请求中?

Has anyone an idea, how I could implement this into a POST request?

直到现在,我都进行了一些绝望的尝试,将过滤器包含在POST请求中(例如raw_result <- POST(url, body = list({ "filter": {"BAS.REFYEAR.v" = 2011}}), encode = "json") 并尝试使用mongolite程序包,该程序包甚至还没有结束.

Until now, I made some desperate attempts to include the filter into the POST requests (e.g. raw_result <- POST(url, body = list({ "filter": {"BAS.REFYEAR.v" = 2011}}), encode = "json") and played around with the mongolitepackage, which was not even close.

更新:过滤问题已解决.我使用以下解决方案:

UPDATE: the filtering problem has been solved. I used the following solution:

myquery <- '{
  "filter": {"BAS.REFYEAR.v": 2015, "BAS.COUNTRY.v": "LV"},
  "fieldIds": {},
  "searchTerms": []
  }'

url <- "https://www.eter-project.com/api/3.0/HEIs/query"

raw_result <- POST(url, body = myquery, content_type_json())

现在,我面临另一个问题:数据包含许多特殊字符,这些特殊字符在R中无法正确显示(例如,数据集中的Alberta koledža显示为Alberta koledžain R).有没有办法解决这个问题,例如在请求调用中使用UTF-8?

Now, I face another problem: the data include many special characters, which are not displayed properly in R (e.g. Alberta koledža in the dataset is displayed as Alberta koledžain R). Is there a way to solve this, for example by using UTF-8 in the request call?

推荐答案

您可以尝试将所需的JSON构建为列表列表.但是,我发现更容易显式提供JSON并手动添加内容类型:

You can try to build the required JSON as list of lists. However, I find it easier to supply the JSON explicitly and add the content type manually:

query <- '{
  "filter": { "BAS.REFYEAR.v": 2011,  "BAS.COUNTRY.v": "AT"},
  "fieldIds": {},
  "searchTerms": []
}'
url <- "https://www.eter-project.com/api/3.0/HEIs/query"
raw_result <- httr::POST(url = url, body = query, content_type_json())

此后,您可以像以前一样应用处理.

After this you can apply your processing as before.

这篇关于R如何向MongoDB发出POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆