html - 如何从 mongodb 获取数据以放入选项?

标签 html node.js mongodb mongoose ejs

我正在创建一个仪表板表来显示 mongoDB 中保存的数据。我已经有一个表并显示表中的所有数据,现在我想要实现的是在数据库表上方创建一个 select 元素 select 元素应该包含所有mongodb 的可用日期。例如,我有 20 个相同的日期 05/25/2019 和 10 个相同的日期 05/26/2019 和 30 个相同的日期 05/29/2019 我只想显示上述日期在 select 元素中提到的三个选项。如果在数据库中添加了另一个日期,该日期也应该显示在 option 上。

我尝试对我的选择选项做与我在表上所做的相同的事情,但当然就像表中的数据一样,它显示所有相同的日期所以我有 60 个选项,其中 30 05/29/201910 的日期相同 05/26/201920 的日期相同与 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/

日期 来自网络应用程序。因此,在用户从应用程序提交表单后,它将保存在我的 mongoDB 中,现在我在数据库中有很多数据,并且许多数据是在相同的日期发送的,我想要实现的是获取保存在数据库中的所有日期如果一些日期相同,它只会显示为一个,并且如果数据库中也添加了日期,则会添加 option。我真的很陌生,所以先谢谢你。

最佳答案

如果我很清楚你的问题,你想从你的 User 模型中找到所有不同的日期。也许您的解决方案是 distinct mongoose 选项。
在你的 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>
<% } %>

关于html - 如何从 mongodb 获取数据以放入选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56372378/

相关文章:

ruby-on-rails - Mongoid:在 has_many 关系中搜索

mongodb - 更新mongodb并返回已完全更改的内容?

javascript - 在javascript的每个函数上获取不存在的textarea id?

javascript - Jquery 与 Magento 原型(prototype)的冲突

file - Node isDirectory() 无法正常工作或者我丢失了一些东西

node.js - Webhook 签名不匹配 (Nodejs)

node.js - 使用 Mocha 读取生态系统变量(单元测试)

html - 整页背景的良好图像尺寸是多少

php - 我如何搜索(在数据库行中)

mongodb - 在 MongoDB 中存储(复杂的)MongoDB 查询