The migration needs
The mongoDB database needs to be migrated from test server A to test server B. MongoDB can be implemented using the export tool Mongoexport and import tool Mongoimport. MongoDB mongoexport, mongoDB mongoimport
Export data
Mongoexport parameters
general options: --help print usage --version print the tool version and exit verbosity options: -v, --verbose=<level> more detailed log output (include multiple times for more verbosity, e.g. -vvvvv, or specify a numeric value, e.g. --verbose=N) --quiet hide all log output connection options: -h, --host=<hostname> mongodb host to connect to (setname/host1,host2 for replica sets) --port=<port> server port (can also use --host hostname:port) authentication options: -u, --username=<username> username for authentication -p, --password=<password> password for authentication --authenticationDatabase=<database-name> database that holds the user's credentials --authenticationMechanism=<mechanism> authentication mechanism to use namespace options: -d, --db=<database-name> database to use -c, --collection=<collection-name> collection to use output options: -f, --fields=<field>[,<field>]* comma separated list of field names (required for exporting CSV) e.g. -f "name,age" --fieldFile=<filename> file with field names - 1 per line --type=<type> the output format, either json or csv (defaults to 'json') -o, --out=<filename> output file; if not specified, stdout is used --jsonArray output to a JSON array rather than one object per line --pretty output JSON formatted to be human-readable querying options: -q, --query=<json> query filter, as a JSON string, e.g., '{x:{$gt:1}}' --queryFile=<filename> path to a file containing a query filter (JSON) -k, --slaveOk allow secondary reads if available (default true) --readPreference=<string>|<json> specify either a preference name or a preference json object --forceTableScan force a table scan (do not use $snapshot) --skip=<count> number of documents to skip --limit=<count> limit the number of documents to export --sort=<json> sort order, as a JSON string, e.g. '{x:1}' --assertExists if specified, export fails if the collection does not exist (false)Copy the code
Export the collection as a.csv file
mongoexport --db users --collection contacts --csv --fieldFile fields.txt --out /opt/backups/contacts.csv
Copy the code
Export the collection as a.json file
mongoexport --db sales --collection contacts --out contacts.json --journal
Copy the code
Mongoimport parameter option
general options:
--help print usage
--version print the tool version and exit
verbosity options:
-v, --verbose=<level> more detailed log output (include multiple
times for more verbosity, e.g. -vvvvv, or
specify a numeric value, e.g. --verbose=N)
--quiet hide all log output
connection options:
-h, --host=<hostname> mongodb host to connect to (setname/host1,host2
for replica sets)
--port=<port> server port (can also use --host hostname:port)
authentication options:
-u, --username=<username> username for authentication
-p, --password=<password> password for authentication
--authenticationDatabase=<database-name> database that holds the user's credentials
--authenticationMechanism=<mechanism> authentication mechanism to use
namespace options:
-d, --db=<database-name> database to use
-c, --collection=<collection-name> collection to use
input options:
-f, --fields=<field>[,<field>]* comma separated list of field names, e.g. -f
name,age
--fieldFile=<filename> file with field names - 1 per line
--file=<filename> file to import from; if not specified, stdin is
used
--headerline use first line in input source as the field
list (CSV and TSV only)
--jsonArray treat input source as a JSON array
--type=<type> input format to import: json, csv, or tsv
(defaults to 'json')
ingest options:
--drop drop collection before inserting documents
--ignoreBlanks ignore fields with empty values in CSV and TSV
--maintainInsertionOrder insert documents in the order of their
appearance in the input source
-j, --numInsertionWorkers=<number> number of insert operations to run concurrently
(defaults to 1)
--stopOnError stop importing at first insert/upsert error
--upsert insert or update objects that already exist
--upsertFields=<field>[,<field>]* comma-separated fields for the query part of
the upsert
--writeConcern=<write-concern-specifier> write concern options e.g. --writeConcern
majority, --writeConcern '{w: 3, wtimeout: 500,
fsync: true, j: true}' (defaults to 'majority')
--bypassDocumentValidation bypass document validation
Copy the code
Mongoimport imports the.csv file
mongoimport --db users --collection contacts --type csv --file /opt/backups/contacts.csv
Copy the code
Mongoimport imports the.json file
mongoimport --collection contacts --file contacts.json --journal
Copy the code
Server instance
Go to the mongodb installation directory of server A, for example, CD /usr/local/mongodb/bin
Data export:
./mongoexport -d DataBaseName -c CollectionName -o bak.dat
Copy the code
DataBaseName is the DataBaseName, CollectionName is the CollectionName, and bak.dat is the exported name
The exported bak.dat will be in the mongoexport directory. Such as:
./mongoexport -d user -c guset -o guset.dat
Copy the code
Export the set guset under the user database to the mongoexport directory and name it guset.dat
Import data
Move the exported data file to another server’s mongo directory
sudo mv /tmp/bak.dat /db/mongo/bin
Copy the code
Note: bak.dat asks the original server to export the data file. The mongoDB installation directory is displayed.
cd /db/mongo/bin
Copy the code
use
/mongoimport -h 127.0.0.1:port -u XXX -p XXX -d DataBaseName -c CollectionName bak.datCopy the code
DataBaseName is the DataBaseName, CollectionName is the CollectionName, and bak.dat is the imported collection
Examples of operation
/mongoimport -h 127.0.0.1:27017 -u user -p user -d guset -c guset bak.datCopy the code
No further action is required.
Note ⚠ ️
One of them says
Do not use mongoimport and mongoexport for full instance, production backups because they will not reliably capture data type information. Use mongodump and mongorestore as described in “Backup Strategies for MongoDB Systems” for this kind of functionality.
That is, do not use Mongoimport and Mongoexport for full instance production backups, as they cannot reliably capture data type information. Use Mongodump and Mongorestore, as described in “Backup Strategy for MongoDB Systems”, to implement such functionality. Mongo mongodump document