MongoDB common database commands corpus

  • 2020-12-10 00:55:36
  • OfStack

1. MongoDB database common operation commands

1, Help view command prompt


help
db.help();
db.yourColl.help();

2. Switch/create database


use raykaeso;

The current database is automatically created when a collection (table) is created

3. Query all databases


show dbs;

4. Delete the database currently in use


db.dropDatabase();

5, clone database from specified host


db.cloneDatabase( " 127.0.0.1 " );

Clones data from the database on the specified machine to the current database

6, copy the specified database data from the specified machine to a database


db.copyDatabase( " mydb " ,  " temp " ,  " 127.0.0.1 " );

Copy the data from the native mydb to the temp database

7. Fix the current database


db.repairDatabase();

8. View the database you are currently using


db.getName()/db;

9. Display the current db status


db.stats();

10. Current VERSION of db


db.version();

11. View the current CONNECTION server machine address of db


use raykaeso;
0

12, query the previous error information and clear


db.getPrevError();

db.resetError();

2. MongoDB Collection aggregate collection

1. Create an aggregate set (table)


db.createCollection( " collName " , {size: 20, capped: 5, max: 100});// Successful creation will be displayed { " ok " :1}

// Determines whether the set is a fixed capacity db.collName.isCapped();

2. Get the aggregation set with the specified name (table)


use raykaeso;
3

3. Get all the aggregate sets of the current db


use raykaeso;
4

4. Display the status of all clustered indexes in db


use raykaeso;
5

5. Query the number of data bars in the current collection


use raykaeso;
6

6. Check the size of the current collection data space


use raykaeso;
7

7. Get db where the current aggregation is located


use raykaeso;
8

8. Get the current state of the aggregation


use raykaeso;
9

9. Get the total size of the aggregation set


db.coll.totalSize();

10. Size of collection storage space


db.coll.storageSize();

11. Rename the aggregate collection


db.coll.renameCollection( " ray " );

Rename coll to ray

12. Delete the current aggregate collection


db.coll.drop();

3. MongoDB User related

1. Add 1 user (create)


db.createUser({user: 'username', pwd: 'xxxx', roles: [{role: 'readWrite', db: 'dbname'}]});

Add user, set password, read only or not

2. Database authentication and Security mode (login)


db.auth( " ray " ,  " 123456 " );

3. Display all current users


show dbs;
6

4. Delete users


show dbs;
7

4. MongoDB aggregate collection query

1. Query all records


show dbs;
8

Equivalent: select* from userInfo;

By default, 20 records are displayed per page. When the display does not work, you can use the it iteration command to query the data of the next page. Note: Typing the it command cannot be followed by ";"

But you can set the size of the data displayed per page, using DBQuery.shellBatchSize = 50; That's 50 records per page.

2, query the deleted duplicate data of a column in the current aggregate collection


show dbs;
9

The same data in name is filtered out

Equivalent to: select distict name from userInfo

3. Query the record of age = 22


db.userInfo.find({ " age " : 22});

select * from userInfo where age = 22;

4. Record of conditional query

The conditional operators in MongoDB are:

( > ) greater than $gt

( < ) less than or equal to $lt ( > $gte =) or greater than or equal to

( < $lte. $lte = $lte


db.dropDatabase();
1

6, character fuzzy query


db.dropDatabase();
2

7. Query the specified column data


db.dropDatabase();
3

Of course, name can also be used true or false

8. Query specified column data according to conditions


db.dropDatabase();
4

9, sorting,

db.userInfo.find().sort({age: 1});

Descending order: db.userInfo.find().sort({age: -1});

10. Query the first 5 data


db.dropDatabase();
5

11. Query the data after 10 pieces


db.dropDatabase();
6

12. Query data between 5 and 10


db.dropDatabase();
7

Can be used for pagination, limit is pageSize, skip is page on pageSize

Equivalent to: select from userInfo limit 5,10;

13. or and query


db.dropDatabase();
8

14. Query the data in article 1


db.userInfo.findOne();

db.userInfo.find().limit(1);

 Is equivalent to: select * from userInfo limit 1;

15. Query the number of records in a result set


db.cloneDatabase( " 127.0.0.1 " );
0

5. MongoDB index

1. Create indexes


db.cloneDatabase( " 127.0.0.1 " );
1

2. Query all indexes of the current collection


db.cloneDatabase( " 127.0.0.1 " );
2

3. View the total index record size


db.cloneDatabase( " 127.0.0.1 " );
3

4. Read all index information of the current collection


db.cloneDatabase( " 127.0.0.1 " );
4

5. Delete the specified index


db.cloneDatabase( " 127.0.0.1 " );
5

6. Delete all indexes


db.cloneDatabase( " 127.0.0.1 " );
6

6. MongoDB modifies, adds and deletes collection data

1, add


db.cloneDatabase( " 127.0.0.1 " );
7

The data column of the added data is not fixed and shall be subject to the added data

2, modify,


db.cloneDatabase( " 127.0.0.1 " );
8

3, delete,


db.cloneDatabase( " 127.0.0.1 " );
9

4. Query, modify and delete


db.copyDatabase( " mydb " ,  " temp " ,  " 127.0.0.1 " );
0

See the links below for more articles on common MongoDB database commands


Related articles: