只显示最近的猫鼬时间戳?

Displaying only the most recent mongoose timestamp?

我有一个 HTML table 填充了 MongoDB 数据。我使用猫鼬时间戳来显示数据库条目更新的任何时间的日期和时间。

我只想显示最近的 date/time,而不是每个更新条目的 date/time(由于 forEach 循环,目前正在发生)。

我对 mongoose 不是很熟悉,所以我想知道是否有办法检查最近的 <%= environment.updatedAt %> 并只显示那个?

HTML:

<table>
   <% environments.forEach(function(environment){ %>

    <%= environment.updatedAt =>

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

   <% }); %> 
 </table>

如果您想查看我的 mongoose 架构或其他任何内容,请告诉我。谢谢!!

如果想在table之后显示最近的updateAt,在循环的时候赋值给一个变量,稍后再显示。假设 updateAt 是数字,那可能看起来像

<%= newest = Math.max(newest, environment.updatedAt) =>

如果您希望首先显示最近的时间,或者在每一行中显示,一个选择是使用 reduce 查找最新的

<%=  environments.reduce(function(a,v){return Math.max(a,v.updatedAt)},0) =>

或者您可以在检索数据时按 updateAt: -1 对查询进行排序,这样最新的就是第一个元素。