In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
How to compress MongoDB data files, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.
MongoDB adopts the mechanism of pre-allocation of disk space, in order to avoid disk fragments and further memory fragments caused by using mmap, but with the addition, deletion and modification of data, the data file will inevitably produce holes, resulting in a waste of disk space and memory.
How to compress the data file of MongoDB
1. Organize data files through repairDatabase
RepairDatabase is a method on db built into MongoDB. When this method is called, MongoDB scans all data in db and reorganizes the data collection by reinserting.
The price of this method is that the read and write operations on this db will become very slow, and even write operations will be lost. Therefore, it is best to stop the write operation of the client directly at this time.
You can create a bar.js file and write the following code:
/ / Getathecurrentcollectionsize.
Varstorage=db.foo.storageSize ()
Vartotal=db.foo.totalSize ()
Print ('StorageSize:'+tojson (storage))
Print ('TotalSize:'+tojson (total))
Print ('-')
Print ('Runningdb.repairDatabase ()')
Print ('-')
/ / Runrepair
Db.repairDatabase ()
/ / Getnewcollectionsizes.
Varstorage_a=db.foo.storageSize ()
Vartotal_a=db.foo.totalSize ()
Print ('StorageSize:'+tojson (storage_a))
Print ('TotalSize:'+tojson (total_a))
Then run:
$mongofoobar.js
Running result:
MongoDBshellversion:1.6.4
Connectingto:foo
StorageSize:51351
TotalSize:79152
Runningdb.repairDatabase ()
StorageSize:40960
TotalSize:65153
How to compress the data file of MongoDB
two。 If the application is acceptable, consider using cappedcollection
Cappedcollection is a special collection in MongoDB, its size can be limited, and the data is written cyclically within this limited size. After the data set reaches the upper limit, the new data will overwrite the old data. In this way, the holes in the disk will be eliminated automatically after a period of time.
In fact, the use of replication-related functions can also achieve the above data compression, the specific process is more troublesome, but also involves the switching of the client and other operations.
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.