最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

给出两个mongodb ID如何确定哪个年龄更大

IT培训 admin 3浏览 0评论

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

评论列表 (0)

  1. 暂无评论