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. iOS图片压缩上传

    本文实例为大家分享了iOS实现压缩图片上传功能,供大家参考,具体内容如下 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 2 ...

  2. jsp学习之scriptlet的使用方法

    scriptlet的使用 jsp页面中分三种scriptlet: 第一种:<%  %>  可以在里面写java的代码.定义java变量以及书写java语句. 第二种:<%! %> ...

  3. spring cloud outh2

    使用Spring Cloud Security OAuth2搭建授权服务http://www.blogjava.net/paulwong/archive/2016/09/16/431797.html? ...

  4. <unistd.h>的close(0)导致std::cout被关闭

    代码如下:其中ZJ::open_max返回系统允许的打开的文件的最大个数 #include "util.h" #include <unistd.h> // int cl ...

  5. 基于jQuery仿Flash横向切换焦点图

    给各网友分享一款基于jQuery仿Flash横向切换焦点图.利用Flash可以制作很多漂亮的图片相册应用,今天我们要用jQuery来实现这样的效果.它是一款仿Flash的横向图片切换焦点图插件,可以自 ...

  6. cocosbuilder的一些坑

    主要是大小写问题 在扁平发布模式下,如果存在大小写不同的文件,文件会被替换掉.而模拟上运行没问题,在真机上运行 有问题.找了半天才发现,坑啊!

  7. wcf客户端终结点样本集合

    1. <bindings> <basicHttpBinding> <binding name="BasicHttpBinding_IUser" /&g ...

  8. [uboot]MLO和uboot-spl.bin, uboot.img和uboot.bin

    前段时间使用TI的am4378芯片,发现系统在SD卡启动的时候,启动文件使用的是MLO和uboot.img:而Norflash和eMMC启动的时候使用的是 uboot-spl.bin和uboot.bi ...

  9. linux - camera capture

    //cut a picture#include <stdio.h>#include <stdlib.h>#include <string.h>#include &l ...

  10. Redis 响应延迟问题排查

    计算延迟时间 如果你正在经历响应延迟问题,你或许能够根据应用程序的具体情况算出它的延迟响应时间,或者你的延迟问题非常明显,宏观看来,一目了然.不管怎样吧,用redis-cli可以算出一台Redis 服 ...