- Permissions
Data security is a critical part of a database, so here's a general procedure for setting up MongoDB user rights.
First, create a new database administrator in unlicensed mode:
Start the database service:
mongod
Enable the Named line tool:
mongouse admindb.createUser({user:"gly",pwd:"[email protected]",roles:[{role:"userAdminAnyDatabase",db:"admin"}]})
Then, create a new user for the specified database:
db.createUser({user:"testdb",pwd:"[email protected]",roles:[{role:"readWrite",db:"testdb"},{role:"userAdmin",db:"testdb"},{role:"dbAdmin",db:"testdb"}]})
Finally, access the data in authorization mode:
mongod -auth
Enable the Named line tool:
mongouse testdbdb.auth("testdb","[email protected]")db.CompanyCards.find({}).limit(1)
Attention:? When setting a password, do not use ' @ ' as much as possible, otherwise you will not be able to connect when you configure the database connection string, such as Connection field string mongodb://testdb:[email protected][email protected]:27017/testdb?authSource=testdb
, ' @ ' as database user name: The delimiter between the password and the database host address, so you cannot use ' @ '; the correct connection string can be configured as: mongodb://testdb:[email protected]:27017/testdb?authSource=testdb
. Both the data and user names in the connection string above are testdb.
- Backup (Export) command:
mongoexport -u UName -p [email protected] -h 127.0.0.1:27017 -d DBName -c CName --type csv -o C:\DBak\CName_201708.csv -f Name,Code,Status
Interpretation:Data Export Tool Mongoexport
Run:Results:
Execution time: about 18 minutes; Export data: 68,668,050; export file: 4,159,994kb; test server: 2 core 8G.
Example of exported file data:
- Restore (Import) command:
mongoimport-h 127.0.0.1:27017-d dbname-c CName--type csv--file C:\DBak\CName_20170800.csv-u uname-p [email protected]--fields name.string (), code.string (), Status.int32 ()--columnshavetypes
When I import, I display fields that specify document, and for the exported data, I don't want the first line because it's the header row. Then there are two ways: one, import directly, then delete, two, delete the title, and then import.
Method One:
Method Two:
Interpretation: Data import Tool Mongoexport
run:
results: Execution time: about 27 minutes; import data: 68,668,050; Import files: 4 , 227,053KB; test server: 2 core 8G.
- To re-index:
use testdbdb.auth("testdb","[email protected]")db.CompanyCards.createIndex( { Name: 1 } )
Command:db.CompanyCards.aggregate([{ $group: { _id: {Name: ‘$Name‘}, count: {$sum: 1}, dups: {$addToSet: ‘$_id‘}}},{ $match: {count: {$gt: 1}}}],{ allowDiskUse: true }).forEach( function(doc){ doc.dups.shift(); db.CompanyCards.remove({_id: {$in: doc.dups}});});
Note: Disk storage is allowed because of the large collection of data that we operate allowDiskUse:true
.
Run:Because it takes a long time, I don't write the result.
The following data are the results of my previous tests and provide references:
? Go to Heavy before: 1,263,765 article
? Go to weight after: 1,258,414 article
? Total time: 53.151 s
? Filter out: 5,351 strips
? Notebook: 4-core 8G,I5 processor.
- More
For more information, see MongoDB official online documentation MongoDB Documention.