8: Preallocate space, whenever possible

原文如下:

 

This is closely related to both “Tip #6: Do not embed fields that have unbound

growth” on page 7 and “Tip #7: Pre-populate anything you can” on page 8. This is an

optimization for once you know that your documents usually grow to a certain size,

but they start out at a smaller size. When you initially insert the document, add a

garbage field that contains a string the size that the document will (eventually) be, then

immediately unset that field:

    > collection.insert({"_id" : 123, /* other fields */, "garbage" : someLongString})

    > collection.update({"_id" : 123}, {"$unset" : {"garbage" : 1}})

This way, MongoDB will initially place the document somewhere that gives it enough

room to grow (Figure 1-3).



看下unset的定义

类似占位符之类的,可能mongoDB对于unset后的字段空间不会删除,只是做一下标记,这样这部分空间已经预先分出来了,这个空间就可以被重用

你可能感兴趣的:(mongodb)