dash1e
dash1e

Reputation: 7807

MongoDB and update-in-place

I have a very large MongoDB object, about 2MB.

I have to update frequently the readCount field and I need to be sure that the operation is very fast.

I know about "update-in-place" and I'm able to send this simple operation

db.pages.update( { name:"SamplePage" }, { $inc: { readCount : 1 } } );

But how MongoDB process that operation internally? It load all the document from disk, modify the value, and store the entire document, or, if the document size does not change, it is able to update on disk only the file part relative to the readCount value?

Upvotes: 6

Views: 4621

Answers (2)

Ashok Mahalik
Ashok Mahalik

Reputation: 51

MongoDB computes padding factor for each collection based on how often items grow or move. More often grow larger padding factor. Internally it uses an adaptive algorithm to try to minimize moves on an update. Basically it operates in RAM.

Upvotes: 0

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230286

MongoDB uses memory-mapped files for its data file management. What this actually means is that mongo doesn't load documents from disk. Instead it tries to access a memory page where that document is located. If that page is not yet in RAM, then the OS goes ahead and fetches it from the disk.

Writing is exactly the same. Mongo tries to write to a memory page. If it's in RAM, then it's ultra-fast (just swapping some bits in the memory). The page is marked dirty and the OS will take care of flushing it back to disk (persisting your changes).

If you have journal enabled, then your inserts/updates are somewhat more expensive, as mongodb has to make another write to the append-only file.

In my app mongodb handles 10-50k updates per second per instance on a modest hardware.

Upvotes: 13

Related Questions