使用 mongoose 和 API 填充 Mongo 数据库
Populate Mongo database with mongoose and API
因此,对于个人项目,我正在尝试制作会议及其论文的数据库。
为此,我正在使用 Mongo DB 社区服务器、node.js、mongoose 和 Elsevier Scopus API。
到目前为止,我已经制作了自己的 JSON 文件,其中包含每个会议的所有相关信息,但是一旦我试图用每年版本的相关论文填充每个会议,我就无法正确检索 JSON 从 Scopus 检索到的文件。
我的数据库的架构如下:
var edition_papers = new mongoose.Schema({
title: String,
author: String,
abstract: String
});
const papers = mongoose.model('papers', edition_papers);
// will each hold a reference to a multiple document from the papers collection
var conference_edition = new mongoose.Schema({
edition: Number,
papers: {
type: mongoose.Schema.Types.ObjectId,
ref: 'papers'
}
});
const edition = mongoose.model('editions', conference_edition);
const ConferenceSchema = new Schema({
acronym : String,
name : String,
area : String,
subarea : String,
location : String,
year: {
type: mongoose.Schema.Types.ObjectId,
ref: 'editions'
}
});
对于我的填充函数:
function apiCall(conf_name, year) {
var url = 'https://api.elsevier.com/content/search/scopus?query=CONFNAME(' + conf_name + ')%20AND%20DOCTYPE(cp)%20AND%20SRCTYPE(p)%20AND%20PUBYEAR%20%3D%20' + year + '&apiKey=' + key;
fetch(url).then(response => response.json()).then(data => {
console.log(data)
})
}
function getPapers(year, conf_name) {
//var json_papers = apiCall(conf_name, year);
var temp = [];
for (let i = 0; i < apiCall(conf_name, year).search-results.totalResults; i++) {
temp[i] = new Papers({
title: json_papers.entry[i].title,
author: json_papers.entry[i].creator,
})
}
return temp;
}
function getEdition(conf_name) {
var years = [2020, 2019, 2018, 2017, 2016, 2015];
var temp = [];
for (var i = 0; i < years.length; i++) {
temp[i] = new Editions({
editions: years[i],
papers: getPapers(years[i], conf_name),
});
}
return temp;
}
function createNewConf(conferences) {
var temp;
temp = new Conference({
acronym: conferences.acronym,
fullname: conferences.fullname,
area: conferences.area,
subarea: conferences.subarea,
location: conferences.location,
year: getEdition(conferences.acronym),
});
return temp;
}
我的问题是,虽然我能够从 API 正确获取 JSON 文档,但文档
看起来像下面这样,我不知道如何使用“。”在它的变量上并有效地循环本文档的条目以从论文中检索相关变量。
{
'search-results': {
'opensearch:totalResults': '62',
'opensearch:startIndex': '0',
'opensearch:itemsPerPage': '25',
'opensearch:Query': {
'@role': 'request',
'@searchTerms': 'CONFNAME(AAAI) AND DOCTYPE(cp) AND SRCTYPE(p) AND PUBYEAR = 2020',
'@startPage': '0'
},
link: [ [Object], [Object], [Object], [Object] ],
entry: [
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object]
]
}
}
//within each object of the entry array is the following :
"prism:url": "https://api.elsevier.com/content/abstract/scopus_id/85081235453",
"dc:identifier": "SCOPUS_ID:85081235453",
"eid": "2-s2.0-85081235453",
"dc:title": "Structural brain changes with lifetime trauma and re-experiencing symptoms is 5-HTTLPR genotype-dependent",
"dc:creator": "Ancelin M.L.",
"prism:publicationName": "European Journal of Psychotraumatology",
"prism:issn": "20008198",
"prism:eIssn": "20008066",
"prism:volume": "11",
"prism:issueIdentifier": "1",
"prism:pageRange": null,
"prism:coverDate": "2020-12-31",
"prism:coverDisplayDate": "31 December 2020",
"prism:doi": "10.1080/20008198.2020.1733247",
"citedby-count": "0",
我尝试了各种方法,但我最大的问题是 ':' 不允许我使用 json_papers.entries。[在此处插入变量] 符号。
有人有我的问题的相关文档或解决方案吗?
非常感谢!
马蒂厄·博纳多
您可以尝试使用方括号表示法访问 json 数据。
在您的示例中,您尝试以点表示法访问:
json_papers.entries.[insert variable here ] notation
要使用方括号表示法,您只需这样做:
json_papers.entries[insert variable here ] notation
举个例子,让你的 json:
const a = {
"prism:url": "https://api.elsevier.com/content/abstract/scopus_id/85081235453",
"dc:identifier": "SCOPUS_ID:85081235453",
"eid": "2-s2.0-85081235453",
"dc:title": "Structural brain changes with lifetime trauma and re-experiencing symptoms is 5-HTTLPR genotype-dependent",
"dc:creator": "Ancelin M.L.",
"prism:publicationName": "European Journal of Psychotraumatology",
"prism:issn": "20008198",
"prism:eIssn": "20008066",
"prism:volume": "11",
"prism:issueIdentifier": "1",
"prism:pageRange": null,
"prism:coverDate": "2020-12-31",
"prism:coverDisplayDate": "31 December 2020",
"prism:doi": "10.1080/20008198.2020.1733247",
"citedby-count": "0"}
我能够通过以下方式访问 json 数据:
a["prism:url"] //"https://api.elsevier.com/content/abstract/scopus_id/85081235453"
因此,对于个人项目,我正在尝试制作会议及其论文的数据库。 为此,我正在使用 Mongo DB 社区服务器、node.js、mongoose 和 Elsevier Scopus API。 到目前为止,我已经制作了自己的 JSON 文件,其中包含每个会议的所有相关信息,但是一旦我试图用每年版本的相关论文填充每个会议,我就无法正确检索 JSON 从 Scopus 检索到的文件。
我的数据库的架构如下:
var edition_papers = new mongoose.Schema({
title: String,
author: String,
abstract: String
});
const papers = mongoose.model('papers', edition_papers);
// will each hold a reference to a multiple document from the papers collection
var conference_edition = new mongoose.Schema({
edition: Number,
papers: {
type: mongoose.Schema.Types.ObjectId,
ref: 'papers'
}
});
const edition = mongoose.model('editions', conference_edition);
const ConferenceSchema = new Schema({
acronym : String,
name : String,
area : String,
subarea : String,
location : String,
year: {
type: mongoose.Schema.Types.ObjectId,
ref: 'editions'
}
});
对于我的填充函数:
function apiCall(conf_name, year) {
var url = 'https://api.elsevier.com/content/search/scopus?query=CONFNAME(' + conf_name + ')%20AND%20DOCTYPE(cp)%20AND%20SRCTYPE(p)%20AND%20PUBYEAR%20%3D%20' + year + '&apiKey=' + key;
fetch(url).then(response => response.json()).then(data => {
console.log(data)
})
}
function getPapers(year, conf_name) {
//var json_papers = apiCall(conf_name, year);
var temp = [];
for (let i = 0; i < apiCall(conf_name, year).search-results.totalResults; i++) {
temp[i] = new Papers({
title: json_papers.entry[i].title,
author: json_papers.entry[i].creator,
})
}
return temp;
}
function getEdition(conf_name) {
var years = [2020, 2019, 2018, 2017, 2016, 2015];
var temp = [];
for (var i = 0; i < years.length; i++) {
temp[i] = new Editions({
editions: years[i],
papers: getPapers(years[i], conf_name),
});
}
return temp;
}
function createNewConf(conferences) {
var temp;
temp = new Conference({
acronym: conferences.acronym,
fullname: conferences.fullname,
area: conferences.area,
subarea: conferences.subarea,
location: conferences.location,
year: getEdition(conferences.acronym),
});
return temp;
}
我的问题是,虽然我能够从 API 正确获取 JSON 文档,但文档 看起来像下面这样,我不知道如何使用“。”在它的变量上并有效地循环本文档的条目以从论文中检索相关变量。
{
'search-results': {
'opensearch:totalResults': '62',
'opensearch:startIndex': '0',
'opensearch:itemsPerPage': '25',
'opensearch:Query': {
'@role': 'request',
'@searchTerms': 'CONFNAME(AAAI) AND DOCTYPE(cp) AND SRCTYPE(p) AND PUBYEAR = 2020',
'@startPage': '0'
},
link: [ [Object], [Object], [Object], [Object] ],
entry: [
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object]
]
}
}
//within each object of the entry array is the following :
"prism:url": "https://api.elsevier.com/content/abstract/scopus_id/85081235453",
"dc:identifier": "SCOPUS_ID:85081235453",
"eid": "2-s2.0-85081235453",
"dc:title": "Structural brain changes with lifetime trauma and re-experiencing symptoms is 5-HTTLPR genotype-dependent",
"dc:creator": "Ancelin M.L.",
"prism:publicationName": "European Journal of Psychotraumatology",
"prism:issn": "20008198",
"prism:eIssn": "20008066",
"prism:volume": "11",
"prism:issueIdentifier": "1",
"prism:pageRange": null,
"prism:coverDate": "2020-12-31",
"prism:coverDisplayDate": "31 December 2020",
"prism:doi": "10.1080/20008198.2020.1733247",
"citedby-count": "0",
我尝试了各种方法,但我最大的问题是 ':' 不允许我使用 json_papers.entries。[在此处插入变量] 符号。
有人有我的问题的相关文档或解决方案吗?
非常感谢! 马蒂厄·博纳多
您可以尝试使用方括号表示法访问 json 数据。
在您的示例中,您尝试以点表示法访问:
json_papers.entries.[insert variable here ] notation
要使用方括号表示法,您只需这样做:
json_papers.entries[insert variable here ] notation
举个例子,让你的 json:
const a = {
"prism:url": "https://api.elsevier.com/content/abstract/scopus_id/85081235453",
"dc:identifier": "SCOPUS_ID:85081235453",
"eid": "2-s2.0-85081235453",
"dc:title": "Structural brain changes with lifetime trauma and re-experiencing symptoms is 5-HTTLPR genotype-dependent",
"dc:creator": "Ancelin M.L.",
"prism:publicationName": "European Journal of Psychotraumatology",
"prism:issn": "20008198",
"prism:eIssn": "20008066",
"prism:volume": "11",
"prism:issueIdentifier": "1",
"prism:pageRange": null,
"prism:coverDate": "2020-12-31",
"prism:coverDisplayDate": "31 December 2020",
"prism:doi": "10.1080/20008198.2020.1733247",
"citedby-count": "0"}
我能够通过以下方式访问 json 数据:
a["prism:url"] //"https://api.elsevier.com/content/abstract/scopus_id/85081235453"