给出两个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()) {...}
给出两个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()) {...}