html - 如何从 mongodb 获取数据以放入选项中?
问题描述
我正在创建一个仪表板表,用于在 mongoDB 中显示保存的数据。我已经有一个表并显示表中的所有数据,现在我想要实现的是select
在数据库表上方创建一个元素,该select
元素应包含来自 mongodb 的所有可用日期。例如,我有 20 个相同的日期05/25/2019
和 10 个相同的日期05/26/2019
和 30 个相同的日期,05/29/2019
我只想显示三个选项,上面的日期在select
元素中表示。如果在数据库中添加了另一个日期,也应该显示在数据库中option
。
我尝试对我的选择选项在表上做同样的事情,但当然就像表中的数据一样,它显示所有相同的日期,所以我有 60 个选项,30
它们的日期 相同,日期相同,05/29/2019
并且是相同的日期10
05/26/2019
20
05/25/2019
这是我的index.js
var express = require("express"),
app = express(),
bodyparser = require("body-parser"),
mongoose = require("mongoose");
mongoose.connect("mongodb://localhost:27017/sample", {useNewUrlParser: true});
app.use(bodyparser.urlencoded({ extended: true }));
app.set("view engine", "ejs");
app.use('/views', express.static('views'));
var nameSchema = new mongoose.Schema({
route : String,
origin : String,
destination : String,
estimatedTimeOfArrival : String,
date : String,
time : String
},{
collection : 'log'
})
var User = mongoose.model("User", nameSchema);
app.get("/", function (req, res) {
res.render("index",{ details: null })
})
app.get("/getdetails", function (req, res) {
User.find({}, function (err, allDetails) {
if (err) {
console.log(err);
} else {
res.render("index", { details: allDetails })
}
});
});
app.listen(1412, "localhost", function () {
console.log("server has started at " + 1412);
})
这是我的index.ejs
<div class="tableFixHead">
<% if(details!=null) { %>
<table id="myTable" >
<thead>
<tr class="header" style=" color: white !important;font-weight:bold;">
<th scope="col">Route</th>
<th scope="col">Origin </th>
<th scope="col">Destination</th>
<th scope="col">Estimated Time of Arrival </th>
<th scope="col">Date </th>
<th scope="col">Time</th>
</tr>
</thead>
<% details.forEach(function(item){ %>
<tbody id="myTable" style="color:black;">
<tr>
<td><%= item.route%></td>
<td><%= item.origin %></td>
<td><%= item.destination%></td>
<td><%= item.estimatedTimeOfArrival %></td>
<td><%= item.date%></td>
<td><%= item.time%></td>
</tr>
</tbody>
<% }) %>
</table>
<% } %>
</div>
和一个示例 html 数据https://jsfiddle.net/indefinite/3yzvemcg/2/
来自dates
网络应用程序。因此,在用户从应用程序提交表单后,它将保存在我的 mongoDB 中,现在我在数据库中有很多数据,并且许多数据是在相同的日期发送的,我想要实现的是获取保存在数据库中的所有日期如果某些日期相同,它将仅显示为一个,并且 option
如果日期也已添加到数据库中,则将被添加。我在这方面真的很新,所以提前谢谢你。
解决方案
如果我很好地理解您的问题,您希望从您的User
模型中找到所有不同的日期。也许您的解决方案是distinct
猫鼬选项。
在你的 index.js 中试试这个:
User.find().distinct('date', function(err, dates) {
// dates are an array of all distinct dates.
if (err) {
console.log(err);
} else {
res.render("index", { dates: dates })
}
});
在你的 ejs 文件中添加这个。
// display select only when 'dates' array is defined.
<% if (locals.dates) { %>
<select name="date" id="dates">
<% dates.forEach(function(date){ %>
<option><%= date %></option>
<% }) %>
</select>
<% } %>
推荐阅读
- php - 正则表达式 | 爆炸 | str_split 匹配标题中的数字
- apache - AH01630:客户端被服务器配置拒绝:/data
- java - 如何在我的角度应用程序中调用和使用spring REST POST API,它返回没有正文的字符串?
- reactjs - 使用 onChange 的带有 event.target.id 的材质 ui 自动完成 ID
- apache-kafka - Apache Kafka 推荐的生产部署策略是什么?
- autodesk-forge - 尝试访问 Data Connector API 时出现“无法访问 API 产品”错误
- javascript - 准备要存储在数据库中的 JSON 数据
- php - Xdebug 2 没有出现在 phpinfo()
- python - 附加到嵌套 JSON 字典的顶部
- javascript - 尝试创建 Chrome 扩展程序以一键呈现整个屏幕 | 谷歌见面会