<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Chan Chen Coding...

    Optimizing Object IDs

    The _id field in a MongoDB document is very important and is always indexed for normal collections. This page lists some recommendations. Note that it is common to use the BSON ObjectID datatype for _id's, but the values of an _id field can be of any type.

    Use the collections 'natural primary key' in the _id field.

    _id's can be any type, so if your objects have a natural unique identifier, consider using that in _id to both save space and avoid an additional index.

    When possible, use _id values that are roughly in ascending order.

    If the _id's are in a somewhat well defined order, on inserts the entire b-tree for the _id index need not be loaded. BSON ObjectIds have this property.

    Store Binary GUIDs as BinData, rather than as hex encoded strings

    BSON includes a binary data datatype for storing byte arrays. Using this will make the id values, and their respective keys in the _id index, twice as small.

    Note that unlike the BSON Object ID type (see above), most UUIDs do not have a rough ascending order, which creates additional caching needs for their index.

    > // mongo shell bindata info: 
    > help misc
        b = new BinData(subtype,base64str)     create a BSON BinData value     
        b.subtype()     the BinData subtype (0..255)
        b.length()     length of the BinData data in bytes
        b.hex()     the data as a hex encoded string
        b.base64()     the data as a base 64 encoded string
        b.toString()
    Extract insertion times from _id rather than having a separate timestamp field.

    The BSON ObjectId format provides documents with a creation timestamp (one second granularity) for free. Almost all drivers implement methods for extracting these timestamps; see the relevant api docs for details. In the shell:

    > // mongo shell ObjectId methods 
    > help misc
        o = new ObjectId() create a new ObjectId
        o.getTimestamp() return timestamp derived from first 32 bits of the OID
        o.isObjectId()
        o.toString()
        o.equals(otherid)
    Sort by _id to sort by insertion time

    BSON ObjectId's begin with a timestamp. Thus sorting by _id, when using the ObjectID type, results in sorting by time. Note: granularity of the timestamp portion of the ObjectID is to one second only.

    > // get 10 newest items 
    > db.mycollection.find().sort({id:-1}).limit(10);


    -----------------------------------------------------
    Silence, the way to avoid many problems;
    Smile, the way to solve many problems;

    posted on 2012-02-27 14:06 Chan Chen 閱讀(254) 評論(0)  編輯  收藏 所屬分類: DB

    主站蜘蛛池模板: 亚洲第一综合天堂另类专 | 亚洲精品无码日韩国产不卡av| 国产免费无码一区二区| 亚洲熟妇无码乱子AV电影| 又大又硬又粗又黄的视频免费看| 国产gav成人免费播放视频| 羞羞漫画页面免费入口欢迎你| 国产又大又黑又粗免费视频| 免费国产a理论片| 久久激情亚洲精品无码?V| www成人免费视频| 亚洲精品无码MV在线观看| 久久国产乱子伦精品免费一 | 久久亚洲日韩精品一区二区三区| 日韩免费无码一区二区三区 | 香蕉视频在线免费看| 亚洲欧洲日产国码无码久久99| 国产在线精品观看免费观看| 亚洲AV无码一区二区乱子伦| 8x8×在线永久免费视频| 在线a亚洲老鸭窝天堂av高清| 青青草国产免费久久久91| 香蕉国产在线观看免费| 亚洲线精品一区二区三区影音先锋 | 成人性生交大片免费看无遮挡| 亚洲狠狠婷婷综合久久蜜芽| 2048亚洲精品国产| 久久国产乱子伦免费精品| 亚洲色欲色欱wwW在线| 亚洲国产婷婷综合在线精品 | 中文字幕亚洲专区| 99视频在线看观免费| 亚洲精品天堂成人片AV在线播放 | 国产无遮挡无码视频免费软件| 亚洲精品午夜久久久伊人| 日韩成人免费视频播放| 中国一级毛片免费看视频| 亚洲AV无码一区二区三区人| 亚洲国产精品激情在线观看 | 四虎永久免费地址在线观看| 青柠影视在线观看免费高清|