MongoDB - Change document structure
I'm working with a database MongoDB and due to the high consumption of reso开发者_高级运维urces (work with a dataset of almost 100GB), I need to shrink the field names of documents (something like "ALTER TABLE").
Is there an easy / automatic to do this?
I think so! Check out $rename
: http://www.mongodb.org/display/DOCS/Updating#Updating-%24rename
Run an update()
on your data set with a query with a bunch of $rename
s and I think that will get you what you want.
There's no built-in way to do this, though you could write a script in your preferred language to do so. Another alternative is to update your application code to rewrite documents to use shorter field names when the documents are accessed, which has the advantage of not requiring downtime or coordination of the script and your application code.
Note that even once you shrink the field names, your data set will remain the same size -- MongoDB will update the documents in place, leaving free space "around" the documents, so you may not see a reduction in your working set size. This may be advantageous if you expect your documents to grow, asMongoDB will update in-place when a document grows if there is enough free space to fit the new document.
Alternatively, you can use the repairDatabase
command, which will shrink your data set. repairDatabase
can be quite slow, and requires quite a bit of free disk space (it has to make a full copy of the entire database). repairDatabase
also locks the entire database, so you should run this during a scheduled maintenance window.
Finally, if you are using version 1.9 or newer, you can use the compact
command. compact
requires less free space than repairDatabase
(it needs about an additional 2 gigabytes of disk space), and operates only on a single collection at a time. compact
locks the database the same way as repairDatabase
, and the same warnings about scheduling compaction during a maintenance window applies.
精彩评论