从 mongodb 文档 _id 字段获取日期和时间 [英] Get date and time from mongodb document _id field

查看:120
本文介绍了从 mongodb 文档 _id 字段获取日期和时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从文档 _id 中获取日期和时间是否是正常做法,因此我不需要在我的文档中添加额外的日期"字段?_id 因某种原因改变了吗?

Is it a normal practice to get date and time from document _id, so i don't need to add additional "date" field to my document? Сan _id change for some reason?

推荐答案

这种做法很好.事实上,许多 10gen 支持的驱动程序都有一些从 ObjectId()/MongoID() 中提取创建日期"的方法.

This practice is fine. In fact, many of the 10gen supported drivers have some method of extracting the "created date" from the ObjectId()/MongoID().

因为某种原因改变了_id?

Сan _id change for some reason?

主要的警告是,您必须使用 MongoDB 生成的 ID 才能使其工作.

The major caveat is that you must use IDs generated by MongoDB for this to work.

在某些情况下,您可能拥有更合适的唯一 ID.在这种情况下,您将不会使用 Mongo 的 ID,因此需要自己存储创建日期.

There are cases where you may have a more appropriate unique ID. In this cases you will not be using Mongo's ID and will therefor need to store the created date on your own.

这篇关于从 mongodb 文档 _id 字段获取日期和时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆