简体   繁体   English

使用 moment.js 指示 mongoDB/mongoose 上次更新的时间?

[英]Using moment.js to indicate when mongoDB/mongoose was last updated?

I have a table populated with data from my mongoDB and I want to use moment.js to indicate the last date/time any entry in the table was edited and updated.我有一个表格,其中填充了来自我的 mongoDB 的数据,我想使用 moment.js 来指示表格中任何条目被编辑和更新的最后日期/时间。

My issue is that moment.js seems to be showing the date/time in real time, rather than only when a database entry was edited.我的问题是 moment.js 似乎实时显示日期/时间,而不仅仅是在编辑数据库条目时。

How do I get it to show the correct date and time?如何让它显示正确的日期和时间?

Mongoose Schema, where I use timestamps for the updatedAt feature: Mongoose Schema,其中我使用时间戳作为updatedAt功能:

var environmentSchema = new mongoose.Schema({
    ePIMS: String,
    codeVersion: String,
    region: String
  }, {
    timestamps: true
  });

HTML where I'm trying to use moment.js HTML 我正在尝试使用 moment.js

<table>
   <%= moment(environments.updatedAt) %> <!-- using it here causes the date/time to update in real time -->

   <% environments.forEach(function(environment){ %>
    <!-- if I use the moment.js code here, it shows it 3 times for all three of the ```td```'s below -->
     <tr>

         <td><%= environment.ePIMS %></td>
         <td><%= environment.codeVersion %></td>
         <td><%= environment.region %></td>

     </tr>
   <% }); %> 
 </table>

By the way, it works when I put the moment.js code in my forEach loop where my mongo collection ( environments ) is being called, but then it shows a date and time for every single entry of my database, and updates those dates/times individually.顺便说一句,当我将 moment.js 代码放入我的 mongo 集合( environments )被调用的 forEach 循环中时,它会起作用,但是它会为我的数据库的每个条目显示日期和时间,并更新这些日期/次单独。 Is there a way to do it this way but only have one date/time for all entries?有没有办法做到这一点,但所有条目只有一个日期/时间?

  • Environments is an array of environment objects and you are trying to get updatedAt of that. Environments 是一组环境对象,您正在尝试对其进行更新。
  • You need to get one environment object and get UpdatedAt of that single object.您需要获得一个环境 object 并获得该单个 object 的 UpdatedAt。
  • So you can put [index] mostly [0] environments[0].updatedAt to get the updatedAt of the first environment.所以你可以把 [index] 主要放在 [0] environments[0].updatedAt来获取第一个环境的 updatedAt 。
  • To get the lastest one go with sort({"updatedAt": -1}) and then environments[0].updatedAt获取最新的 go 与sort({"updatedAt": -1})然后environments[0].updatedAt

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

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