【发布时间】:2019-10-15 18:18:56
【问题描述】:
我正在创建一个仪表板表,用于在 mongoDB 中显示保存的数据。我已经有一个表并显示表中的所有数据,现在我想要实现的是在数据库表上方创建一个select 元素,select 元素应该包含来自 mongodb 的所有可用日期。例如,我有 20 个相同的日期 05/25/2019 和 10 个相同的日期 05/26/2019 和 30 个相同的日期 05/29/2019 我只想显示上面在 select 元素中提到的三个选项。如果在数据库中添加了另一个日期,也应该在option 上显示。
我尝试对我的选择选项在表格上做同样的事情,但当然就像表格中的数据一样,它显示所有相同的日期,所以我有 60 个选项,其中30 与 @ 的日期相同987654330@ 和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。我真的很陌生,所以提前谢谢你。
【问题讨论】:
-
如何在 mongoDB 中维护一个单独的查找表,日期已经在
name表中,以便您只需查看该查找表? -
@cadenzah 抱歉,我是新手。你的意思是我手动添加日期?
-
就像你有
User模型来为用户保存数据,维护一个模型,比如说Date模型,每次你在User模型中添加一个新用户,你也检查Date模型并查看该新用户的日期是否已经在Date模型中。这可能看起来效率很低,因为您每次添加新用户时都会检查Date模型,但我想这会满足您的需求。 -
@cadenzah
dates来自网络应用程序。因此,在用户从应用程序提交表单后,它将保存在我的 mongoDB 中,现在我在数据库中有很多数据,并且许多数据是在相同的日期发送的,我想要实现的是获取保存在数据库中的所有日期如果某些日期相同,它将仅显示为一个而不是全部
标签: html node.js mongodb mongoose ejs