<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 閱讀(255) 評論(0)  編輯  收藏 所屬分類: DB

    主站蜘蛛池模板: 亚洲福利精品一区二区三区| 一色屋成人免费精品网站| 免费一级毛片在线播放| 色屁屁在线观看视频免费| 国产午夜鲁丝片AV无码免费| 亚洲精华国产精华精华液好用| 手机在线毛片免费播放 | 亚洲av综合avav中文| 久久国产精品免费一区| 国产午夜亚洲精品理论片不卡| A国产一区二区免费入口| 国产亚洲精品久久久久秋霞| 国产免费一区二区三区不卡 | 国内精品久久久久影院亚洲| 成人在线视频免费| 校园亚洲春色另类小说合集| 亚洲国产精品不卡毛片a在线| 中文字幕免费观看视频| 亚洲A∨无码无在线观看| 91在线视频免费91| 国产精品亚洲精品日韩电影| 国产日产亚洲系列最新| 99免费在线观看视频| 狠狠色香婷婷久久亚洲精品| 免费一看一级毛片| 日韩人妻一区二区三区免费| 亚洲偷偷自拍高清| 亚洲午夜无码AV毛片久久| 日韩精品极品视频在线观看免费| 亚洲国产成人91精品| 又粗又大又长又爽免费视频| 中文在线观看国语高清免费| 亚洲成电影在线观看青青| 国产又粗又长又硬免费视频| 久久aⅴ免费观看| 亚洲欧美日韩久久精品| 亚洲午夜国产精品无码| 国产香蕉九九久久精品免费| eeuss影院免费直达入口| 亚洲三级视频在线| 亚洲老妈激情一区二区三区|