首页 > 解决方案 > 如何从 mongodb 获取数据以放入选项中?

问题描述

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

我尝试对我的选择选项在表上做同样的事情,但当然就像表中的数据一样,它显示所有相同的日期,所以我有 60 个选项,30它们的日期 相同,日期相同,05/29/2019并且是相同的日期1005/26/20192005/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如果日期也已添加到数据库中,则将被添加。我在这方面真的很新,所以提前谢谢你。

标签: htmlnode.jsmongodbmongooseejs

解决方案


如果我很好地理解您的问题,您希望从您的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>
<% } %>

推荐阅读