假设我们有:
const a = ObjectId()
const b = ObjectId()
我们可以做 if(a > b) {...}
如果它们是字符串呢?
const a = String(ObjectId())
const b = String(ObjectId())
我们应该使用 localComapre 还是 if > 仍然足以比较哪个更旧/更年轻?
最佳答案
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()) {...}
关于node.js - 给定两个 mongodb Id 如何确定哪个更旧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58240787/