Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB and update-in-place

Tags:

mongodb

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?

like image 215
dash1e Avatar asked Apr 07 '12 06:04

dash1e


People also ask

How does update work in MongoDB?

MongoDB Update method is used to update the document from the collection, and the update method will update the value of the existing document. We have used a $set operator at the time of updating the document. We can update a single document by using an update or updateOne method.

Does MongoDB update automatically?

No, there is no such thing (autoupdate) in MongoDB. It must be done with application or script.

What is the use of update in MongoDB?

The update () method updates the values in the existing document in the collections of MongoDB. When you update your document the value of the _id field remains unchanged. By default, the db.collection.update () method updates a single document. Include the option multi: true to update all documents that match the given query.

How to update multiple documents in MongoDB with different titles?

Following example will set the new title 'New MongoDB Tutorial' of the documents whose title is 'MongoDB Overview'. By default, MongoDB will update only a single document. To update multiple documents, you need to set a parameter 'multi' to true. The save () method replaces the existing document with the new document passed in the save () method.

How to use findoneandupdate() method in MongoDB?

MongoDB findOneAndUpdate() method. The findOneAndUpdate() method updates the values in the existing document. Syntax. The basic syntax of findOneAndUpdate() method is as follows − >db.COLLECTION_NAME.findOneAndUpdate(SELECTIOIN_CRITERIA, UPDATED_DATA) Example

How do I change a specific field in MongoDB?

MongoDB is a document-oriented NoSQL database that is publicly available. We can update the documents in a collection using various methods like update, replace and save . In order to change a specific field of the document, we'll use different operators like $set, $inc, etc.


Video Answer


1 Answers

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.

like image 135
Sergio Tulentsev Avatar answered Sep 30 '22 13:09

Sergio Tulentsev