我正在修改当前设置为从JSON文件中提取问题,提示和答案的闪存卡应用程序。我想从Mongo数据库中提取所有这些信息。
我有一个名为' flashcards'还有一个名为“卡片”的系列。已在MongoDB本地设置。卡片'集合中已经有五个文件 - 每个文件都有一个问题,一个提示和一个答案。
我有一个这样的路线设置:localhost:5000/${id}?side=${question/answer}
我希望用id和问题或答案填充它,根据我在&#39中相关文档的ID显示问题或答案;卡'收集在Mongo。
从JSON中提取每个问题都很简单,您只需设置一个需要JSON文件的变量,然后您可以使用点表示法或括号表示法访问每个元素。即:
const cards = require('./data/flashcardData.json');
一般格式如下:
cards[id][side];
抓住第一张牌和问题方的具体例子:
cards[0][question];
JSON供参考:
{
"data": {
"title": "JavaScript Flashcards",
"cards": [
{
"question": "What language are Express apps written in?",
"hint": "It starts with a \"J\"",
"answer": "JavaScript"
},
{
"question": "What is one way a website can store data in a user's browser?",
"hint": "They are delicious with milk",
"answer": "Cookies"
},
{
"question": "What is a common way to shorten the response object's name inside middleware?",
"hint": "It has the same abbreviation as \"resolution\"",
"answer": "res"
},
{
"question": "How many different values can booleans have?",
"hint": "Think: binary",
"answer": "2"
},
{
"question": "Which HTML element can contain JavaScript?",
"hint": "It starts with an \"s\"",
"answer": "<script>"
}
]
}
}
以下是&#39;卡片&#39;收集在&#39;抽认卡&#39;数据库看起来与上面相同的信息:
{
"_id": ObjectId("5a8b3084c926c61f9f90b61a"),
"question": "What language are Express apps written in?",
"hint": "It starts with a \"J\" ",
"answer": "Javascript"
}
{
"_id": ObjectId("5a8b31eac5fcfe1fc032ceda"),
"question": "What is one way a website can store data in a user's browser?",
"hint": "They are delicious with milk",
"answer": "Cookies"
}
{
"_id": ObjectId("5a8b31eac5fcfe1fc032cedb"),
"question": "What is a common way to shorten the response object's name inside middleware?",
"hint": "It has the same abbreviation as \"resolution\" ",
"answer": "res"
}
{
"_id": ObjectId("5a8b31eac5fcfe1fc032cedc"),
"question": "How many different values can booleans have?",
"hint": "Think binary",
"answer": "2"
}
{
"_id": ObjectId("5a8b3264e122b41fce2fbcbd"),
"question": "Which HTML element can contain JavaScript?",
"hint": "It starts with an \"s\" ",
"answer": "<script>"
}
我如何使用Mongo复制以前发生的事情?