node.js - 给定两个 mongodb Id 如何确定哪个更旧
问题描述
假设我们有:
const a = ObjectId()
const b = ObjectId()
我们能不能做 if(a > b) {...}
如果它们是字符串呢?
const a = String(ObjectId())
const b = String(ObjectId())
我们应该使用 localComapre 还是 > 仍然足以比较哪个更老/更年轻?
解决方案
ObjectId
对象包含时间戳,您可能想要比较它们。
const a = new ObjectId()
const b = new ObjectId()
if(a.getTimestamp() > b.getTimestamp()) {...}
字符串相同:
const a = String(new ObjectId())
const b = String(new ObjectId())
if((new ObjectId(a)).getTimestamp() > (new ObjectId(b)).getTimestamp()) {...}
推荐阅读
- react-native - 更新 graphql 列中的 JSON 数据
- emotion - 如何合并样式标签
- javascript - 节点csv解析返回值在调用时返回为未定义
- graphql - GraphQL 格式返回对象
- swiftui - replaceOccurrences() 用于通过正则表达式将文本拆分为多行
- javascript - 为什么我的承诺链仍然同时执行?
- pine-script - 松脚本 - 有没有办法获得当天的收盘价,以将其用于过去的收盘系列计算[]?
- django - Django、Nginx、uWSGI sock - 内部服务器错误
- r - 下一行正在谈论的错误是什么?
- azure-powershell - 将 Azure 管道变量作为 Powershell 脚本参数发送