BSON Types — MongoDB Manual https://docs.mongodb.com/manual/reference/bson-types/#objectid

ObjectId

ObjectIds are small, likely unique, fast to generate, and ordered. ObjectId values consist of 12 bytes, where the first four bytes are a timestamp that reflect the ObjectId’s creation. Specifically:

  • a 4-byte value representing the seconds since the Unix epoch,
  • a 5-byte random value, and
  • a 3-byte counter, starting with a random value.

In MongoDB, each document stored in a collection requires a unique _id field that acts as a primary key. If an inserted document omits the _id field, the MongoDB driver automatically generates an ObjectId for the _id field.

This also applies to documents inserted through update operations with upsert: true.

MongoDB clients should add an _id field with a unique ObjectId. Using ObjectIds for the _id field provides the following additional benefits:

  • in the mongo shell, you can access the creation time of the ObjectId, using the ObjectId.getTimestamp() method.

  • sorting on an _id field that stores ObjectId values is roughly equivalent to sorting by creation time.

    IMPORTANT

    While ObjectId values should increase over time, they are not necessarily monotonic. This is because they:

    • Only contain one second of temporal resolution, so ObjectId values created within the same second do not have a guaranteed ordering, and
    • Are generated by clients, which may have differing system clocks.

SEE ALSO

ObjectId()

ObjectId的更多相关文章

  1. MongoDB数据实体中的ObjectId序列化成json的一个小坑

    很多时候我们都需要对实体对象进行序列化与反序列化,而ObjectId类型,只能用mongo的驱动以bson格式来序列化,但我们对外的交互,一般是json格式. 有两种解决方案: 1.自己写一个json ...

  2. iOS CoreData 中 objectID 的不变性

    关于 CoreData的 objectID 官方文档有这样的表述:新建的Object还没保存到持久化存储上,那么它的objectID是临时id,而保存之后,就是持久化的id,不会再变化了. 那么,我想 ...

  3. MongoDB学习笔记~ObjectId主键的设计

    回到目录 说一些关于ObjectId的事 MongoDB确实是最像关系型数据库的NoSQL,这在它主键设计上可以体现的出来,它并没有采用自动增长主键,因为在分布式服务器之间做数据同步很麻烦,而是采用了 ...

  4. MongoDB的ObjectId和基本操作增删改查(3)

    ObjectId 基本操作增删改查 增: insert 介绍: mongodb存储的是文档,. 文档是json格式的对象. 语法: db.collectionName.insert(document) ...

  5. spring mvc fastJson 自定义类型转换(返回数据) 实现对ObjectId类型转换

    json用的alibaba fastJson ValueFilter filter = new ValueFilter() { @Override public Object process(Obje ...

  6. ios coredata NSManagedObject 的 objectID

    要使用这个属性一定要注意先把数据保存下,不然会变化的!就无法通过 - (NSManagedObject*)existingObjectWithID:(NSManagedObjectID*)object ...

  7. ObjectID和Handle

    一个dwg对应一个arx database,也就是一套9个符号表和一个有名词典. 一个CAD session中是可以加载多个database的.加载后每个对象都有一个handle和一个objectid ...

  8. java 查询 mongodb 中的objectid

    网上找了很久查询objectid的方法都是错的,用mongovue能查询出来,但就是用java不知道怎么查询 1.mongovue里的查询方式: {"_id" : ObjectId ...

  9. mongo里面根据对象字段的ID查询 db.Photo.find({'owner.$id':ObjectId('xxxx')}) , 并且使用forEach循环修改查询的数据

    var ones = db.Photo.find({'owner.$id':ObjectId("5344f0dab7c58e8e098b4567")}) db.Photo.find ...

  10. MongoDB ObjectId

    概述 > db.col.find() { , } { , } { , } { , } 每个文档中都有一个“_id”,她是一个12字节的BSON类型数据,格式如下 56c56dd4ca446fab ...

随机推荐

  1. 超NB的WINDWS命令 当插移动硬盘时 自动弹出要格式化硬盘

    其中d表示无法识别的那个盘的盘符 移动磁盘修复成功

  2. Java Main如何被执行?

    java应用程序的启动在在/hotspot/src/share/tools/launcher/java.c的main()函数中,而在虚拟机初始化过程中,将创建并启动Java的Main线程.最后将调用J ...

  3. Java并发之彻底搞懂偏向锁升级为轻量级锁

    网上有许多讲偏向锁,轻量级锁的文章,但对偏向锁如何升级讲的不够明白,有些文章还相互矛盾,经过对jvm源码(biasedLocking.cpp)的仔细分析和追踪,基本升级过程有了一个清晰的过程,现将升级 ...

  4. [原]零基础学习SDL开发之移植SDL2.0到Android

    在[原]SDL开发教程我们知道了如何在pc下使用SDL进行开发,在android上面是否一样可以使用呢?答案是肯定的. 下面我们进行移植SDL到Android,这里都是基于SDL最新版进行移植的,在E ...

  5. spoj687(后缀数组)

    http://www.spoj.com/problems/REPEATS/ 题意:给一串字符,需要你求这一串字符中有连续重复的字符的重复次数....... 思路:这是和poj3693一种类型的题目.. ...

  6. php读取csv的问题

    csv文件要用utf-8 无bom格式保存 如果有英文外的字符,另外每项要用双引号,不用双引号不能保存非英文字符

  7. ajax——用ajax写用户注册

    zhuce.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://w ...

  8. PHP——分页显示的完善(加查询,用类简化sql语句)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. 调用半截的div

    不能引用jquery: <script src="${rootUrl }js/jquery/jquery.js" type="text/javascript&quo ...

  10. datagrid columns

    columns: [[ { field: 'Source_Id', title: 'Source_Id', hidden: true }, //{ field: 'Current_Value', hi ...