> db.fsyncLock()
{
"info" : "now locked against writes, use db.fsyncUnlock() to unlock",
"seeAlso" : "http://dochub.mongodb.org/core/fsynccommand",
"ok" : 1
}
> db.fsyncUnlock()
{ "info" : "unlock completed", "ok" : 1 }
[root@mongodb3 ~]# mongodump --help
Usage:
mongodump <options>
Export the content of a running server into .bson files.
Specify a database with -d and a collection with -c to only dump that database or collection.
See http://docs.mongodb.org/manual/reference/program/mongodump/ for more information.
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
query options:
-q, --query= query filter, as a JSON string, e.g., '{x:{$gt:1}}'
--queryFile= path to a file containing a query filter (JSON)
--readPreference=<string>|<json> specify either a preference name or a preference json object
--forceTableScan force a table scan
output options:
-o, --out=<directory-path> output directory, or '-' for stdout (defaults to 'dump')
--gzip compress archive our collection output with Gzip
--repair try to recover documents from damaged data files (not supported by all storage engines)
--oplog use oplog for taking a point-in-time snapshot
--archive=<file-path> dump as an archive to the specified path. If flag is specified without a value, archive is written to stdout
--dumpDbUsersAndRoles dump user and role definitions for the specified database
--excludeCollection=<collection-name> collection to exclude from the dump (may be specified multiple times to exclude additional collections)
--excludeCollectionsWithPrefix=<collection-prefix> exclude all collections from the dump that have the given prefix (may be specified multiple times to exclude additional prefixes)
-j, --numParallelCollections= number of collections to dump in parallel (4 by default)
[root@mongodb3 ~]# mongodump --host 127.0.0.1 --port 37017
2016-06-27T21:29:49.121+0800 writing admin.system.users to
2016-06-27T21:29:49.121+0800 done dumping admin.system.users (3 documents)
2016-06-27T21:29:49.121+0800 writing admin.system.roles to
2016-06-27T21:29:49.121+0800 done dumping admin.system.roles (2 documents)
2016-06-27T21:29:49.122+0800 writing admin.system.version to
2016-06-27T21:29:49.122+0800 done dumping admin.system.version (1 document)
2016-06-27T21:29:49.122+0800 writing suq.t2 to
2016-06-27T21:29:49.122+0800 writing suq.t1 to
2016-06-27T21:29:49.123+0800 done dumping suq.t2 (1 document)
2016-06-27T21:29:49.123+0800 done dumping suq.t1 (0 documents)
[root@mongodb3 ~]# ls dump
admin suq
[root@mongodb3 ~]# mongodump --port 37017 -o backup/
[root@mongodb3 ~]# mongodump --port 37017 -d suq -o backup
2016-06-27T21:35:00.406+0800 writing suq.t2 to
2016-06-27T21:35:00.406+0800 writing suq.t1 to
2016-06-27T21:35:00.406+0800 done dumping suq.t2 (1 document)
2016-06-27T21:35:00.407+0800 done dumping suq.t1 (0 documents)
[root@mongodb3 ~]# ls
anaconda-ks.cfg backup Desktop Documents Downloads install.log install.log.syslog mm mongod.conf Music Pictures Public soft Templates Videos
[root@mongodb3 ~]# ls backup
suq
[root@mongodb3 ~]# mongodump --port 37017 -c t1 -d suq -o backup
2016-06-27T22:10:43.047+0800 writing suq.t1 to
2016-06-27T22:10:43.047+0800 done dumping suq.t1 (0 documents)
[root@mongodb3 ~]# ls backup
suq
[root@mongodb3 ~]# ls backup/suq/
t1.bson t1.metadata.json
[root@mongodb3 ~]# mongorestore --help
Usage:
mongorestore <options> <directory or file to restore>
Restore backups generated with mongodump to a running server.
Specify a database with -d to restore a single database from the target directory,
or use -d and -c to restore a single collection from a single .bson file.
See http://docs.mongodb.org/manual/reference/program/mongorestore/ for more information.
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:
--objcheck validate all objects before inserting
--oplogReplay replay oplog for point-in-time restore
--oplogLimit=<seconds>[:ordinal] only include oplog entries before the provided Timestamp
--archive=<filename> restore dump from the specified archive file. If flag is specified without a value, archive is read from stdin
--restoreDbUsersAndRoles restore user and role definitions for the given database
--dir=<directory-name> input directory, use '-' for stdin
--gzip decompress gzipped input
restore options:
--drop drop each collection before import
--writeConcern=<write-concern> write concern options e.g. --writeConcern majority, --writeConcern '{w: 3, wtimeout: 500, fsync: true, j: true}' (defaults to 'majority')
--noIndexRestore don't restore indexes
--noOptionsRestore don't restore collection options
--keepIndexVersion don't update index version
--maintainInsertionOrder preserve order of documents during restoration
-j, --numParallelCollections= number of collections to restore in parallel (4 by default)
--numInsertionWorkersPerCollection= number of insert operations to run concurrently per collection (1 by default)
--stopOnError stop restoring if an error is encountered on insert (off by default)
--bypassDocumentValidation bypass document validation
[root@mongodb3 ~]# mongorestore --port 37017 backup/
2016-06-27T22:18:27.571+0800 building a list of dbs and collections to restore from backup dir
2016-06-27T22:18:27.572+0800 reading metadata for suq.t1 from backup/suq/t1.metadata.json
2016-06-27T22:18:27.572+0800 restoring suq.t1 from backup/suq/t1.bson
2016-06-27T22:18:27.572+0800 restoring indexes for collection suq.t1 from metadata
2016-06-27T22:18:27.572+0800 finished restoring suq.t1 (1 document)
2016-06-27T22:18:27.572+0800 done
[root@mongodb3 log]# mongoexport --help
Usage:
mongoexport <options>
Export data from MongoDB in CSV or JSON format.
See http://docs.mongodb.org/manual/reference/program/mongoexport/ for more information.
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}'
[root@mongodb3 test]# mongoexport --port 37017 -d suq -c t1 --type=csv -f name,age -o 1.csv
2016-06-28T20:01:23.951+0800 connected to: localhost:37017
2016-06-28T20:01:23.951+0800 exported 2 records
[root@mongodb3 test]# cat field.txt name age
[root@mongodb3 test]# mongoexport --port 37017 -d suq -c t1 --type=csv --fieldFile field.txt -o 1.csv
2016-06-28T20:08:55.504+0800 connected to: localhost:37017
2016-06-28T20:08:55.505+0800 exported 2 records
[root@mongodb3 test]# mongoexport --port 37017 -d suq -c t1 -o suq.json
2016-06-28T20:12:11.756+0800 connected to: localhost:37017
2016-06-28T20:12:11.756+0800 exported 2 records
[root@mongodb3 test]# cat suq.json
{"_id":{"$oid":"577266483ef351522beaefe4"},"name":"tony","age":10.0}
{"_id":{"$oid":"577266603ef351522beaefe5"},"name":"tony","age":10.0,"sex":1.0}
[root@mongodb3 test]# mongoexport --port 37017 -d suq -c t1 --query '{"sex":1}' -o suq.json
2016-06-28T20:14:00.656+0800 connected to: localhost:37017
2016-06-28T20:14:00.657+0800 exported 1 record
[root@mongodb3 test]# mongoimport --help
Usage:
mongoimport <options> <file>
Import CSV, TSV or JSON data into MongoDB. If no file is provided, mongoimport reads from stdin.
See http://docs.mongodb.org/manual/reference/program/mongoimport/ for more information.
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
[root@mongodb3 test]# mongoimport --port 37017 -d suq -c suq --file suq.json
2016-06-28T20:40:25.148+0800 connected to: localhost:37017
2016-06-28T20:40:25.176+0800 imported 1 document
[root@mongodb3 test]# mongoimport --port 37017 --type=csv -d suq -c suq2 --headerline --file= 1.csv
2016-06-28T20:45:28.430+0800 connected to: localhost:37017
2016-06-28T20:45:28.458+0800 imported 2 documents