MongoDB Learning Management

Source: Internet
Author: User

1. Clone collection
1) clone the remote colletion and run the clonecollection command to copy the remote collection to the local computer.
Command Format: DB. runcommand ({clonecollection: "set", from: "original machine", copyindexes: false}), copyindexes: whether to copy the index
Example: there is a data entry in Table T1 of the test database on 132.42.33.175.
> DB. t1.find ()
{"_ Id": objectid ("4fd9a4bf186cb1b6ac95907d"), "name": "liangzhangping", "ADDR": "Beijing "}
The T1 table in the test database on 132.42.33.190 has two pieces of data.
> DB. t1.find ()
{"_ Id": objectid ("4fd9c517dcde2d0e33d08c76"), "name": "liangzhangping", "Age": 28}
{"_ Id": objectid ("4fda1795a3d56c6a40f2bc26"), "name": "liangzhangping", "ADDR": "Jiangxi "}

Now, we will clone the data from Table T1 of the test database on 132.42.33.175 to table T1 of the test database on 132.42.33.190. The operation is as follows:
A. log on to the 132.42.33.190 machine and run the following command:
> DB. runcommand ({clonecollection: "test. T1", from: "132.42.33.175: 28010 "})
{"OK": 1}
B. View Verification
> DB. t1.find ()
{"_ Id": objectid ("4fd9c517dcde2d0e33d08c76"), "name": "liangzhangping", "Age": 28}
{"_ Id": objectid ("4fda1795a3d56c6a40f2bc26"), "name": "liangzhangping", "ADDR": "Jiangxi "}
{"_ Id": objectid ("4fd9a4bf186cb1b6ac95907d"), "name": "liangzhangping", "ADDR": "Beijing "}

2) clone the local collection. MongoDB does not provide commands for local replication, but we can write a Circular Insertion Method to complete it,
For example, copy the data in source_collection to target_collection. The Code is as follows:
Db.source_collection.find().foreach(function(xw.mongodb.tar get_collection.insert (x )})

2. Copy the database and run the copydatabase command to copy the database,
Format: copydatabase (fromdb, todb, fromhost [, username, password])
Fromdb: source database name
Todb: name of the target database
Fromhost: source database address, both local and remote
Username: Remote database username
Password: Remote Data Password
Example: copy the local DB2 database and rename db1
> DB. copydatabase ("DB2", "db1", "localhost ")

3. Refresh the disk: write information not written to the disk in the memory to the disk and lock the database update operation. However, read operations can be performed using the runcommand command, this command can only be executed on the admin Database
Format: DB. runcommand ({fsync: 1, async: true })
Async: asynchronous execution
Lock: 1 lock the database

4. Data Compression: The storage structure of MongoDB adopts a pre-allocation mechanism. Long-term operations will leave too many fragments, resulting in slower and slower database systems.
The repairdatabase command is a built-in method of MongoDB. It scans all the data in the database and reorganizes the data set by importing/exporting the data to clean up the fragments.
Now let's look at the comparison data before and after compression, as shown below:
Primary> dB. t1.storagesize ()
65232896
Primary> dB. t1.totalsize ()
81470432
Primary> dB. repairdatabase ()
{"OK": 1}
Primary> dB. t1.storagesize ()
65232896
Primary> dB. t1.totalsize ()
79851584

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.