简体   繁体   English

使用ExpressJ在MongoDB中获取数据请求

[英]Get Request for data in MongoDB with ExpressJs

I am new to nodejs/web app and I am trying to get data out from MongoDB. 我是nodejs / web应用程序的新手,我正在尝试从MongoDB中获取数据。 My mongoDB has documents under collection "a" 我的mongoDB的文档集合为“ a”

{_id:("1"), "Question":"mcq1", "Answer":"one", "Keyword":"CLASS"}
{_id:("2"), "Question":"mcq2", "Answer":"two", "Keyword":"CLASS"}
{_id:("3"), "Question":"mcq3", "Answer":"three", "Keyword":"OVERLOAD"}
{_id:("4"), "Question":"mcq4", "Answer":"four", "Keyword":"OODP"}

I want to extract the data "Question": field_value, "Answer":field_value out using nodejs -> expressjs through a button and textbox form using the unique Keyword and to be displayed in a table form as below. 我想使用唯一的关键字通过按钮和文本框形式使用nodejs-> expressjs提取数据“问题”:field_value,“ Answer”:field_value,并以表格形式显示,如下所示。

<tr>
<td><%= Question %></td>
<td><%= Answer %> </td>
</tr>

I have been able to get what i want with the monogodb shell using 我已经能够使用monogodb shell获得我想要的东西

db.a.find({"Keyword":CLASS},{"Question":1,"Answer":1,_id:0})

Currently the textbox and button onclick codes are below. 当前,文本框和按钮onclick代码在下面。

Input Keyword to search: <input type="text" id="searchBtn" value="">
<button type="button" onclick="alert(document.getElementById('searchBtn').value)">Click me!</button>

How can i extract the Question and Answer with the button click? 单击按钮如何提取问题和答案?
Using db.a.find({"Keyword":CLASS},{"Question":1,"Answer":1,_id:0}) i want to get a table in the form of 我想使用db.a.find({"Keyword":CLASS},{"Question":1,"Answer":1,_id:0})来获取以下形式的表格

Question, Answer,Keyword 问题,答案,关键字
mcq1, one, CLASS mcq1,一级
mcq2, two, CLASS mcq2,两个,CLASS

If you do a db query based on that keyword you'll get the occurrences in mongo, so you can do a form(GET/POST) field with the input and the button you already have. 如果您基于该关键字进行数据库查询,则会得到mongo中的出现,因此您可以使用输入和已有的按钮来进行form(GET / POST)字段。 This will be caught in your express code as a route, there you can implement some basic code filling your search needs and the return value will be some simple data or an array if multiple match. 这将作为路由捕获在您的快速代码中,您可以在其中实现一些基本代码来满足您的搜索需求,并且返回值将是一些简单数据或多个匹配的数组。

This is a basic search that one user (Miqe) once taught me here, first you need the query (or you can just put directly there) and later search in the mongo. 这是一个基本搜索,一个用户(Miqe)曾经在这里教过我,首先您需要查询(或者您可以直接将其放在此处),然后在mongo中搜索。 But pay attention that your callback function will return the result, here is just a console.log() after returning the values you can assign them to a variable and pass through a template engine rendering in the html in the desire format. 但是请注意,您的回调函数将返回结果,这只是返回值之后的console.log(),您可以将它们分配给变量,并以所需格式通过html中的模板引擎呈现。

query = {username: username, password: password}
dbo.collection('Users').find(query, function(err, user){
  if(err) throw new Error(err);
  if(!user) 
    console.log('Not found');
  else 
    console.log('Found!');

}) })

Here is just the code to find in a collection named Users, you still need to join with the route and do a connection to the DB. 这只是在名为Users的集合中找到的代码,您仍然需要加入路由并建立与数据库的连接。

I'll leave a link who helped me a lot! 我将留下一个对我有很大帮助的链接! The mongo documentation is still a good start too. mongo文档仍然是一个不错的开始。 https://www.w3schools.com/nodejs/nodejs_mongodb_query.asp https://www.w3schools.com/nodejs/nodejs_mongodb_query.asp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM