After the successful start of MongoDB, and then open a command line window input MONGO, you can do some of the database operations. Enter help to see the basic Operations Command, but MongoDB does not create a database command, but there are similar commands such as: If you want to create a "myTest" database, first run the use MyTest command, Then do something like db.createcollection (' user ') so you can create a database called "MyTest".
One, the database Common command
1, help view command prompt
Copy code code as follows: Help
Db.help ();
Db.yourColl.help ();
Db.youColl.find (). Help ();
Rs.help ();
2, switch/CREATE DATABASE
Copy the code as follows: use YOURDB; When creating a collection (table), the current database
3, querying all databases
is copied code as follows: Show DBS;
4. Delete the currently used database
Copy code code as follows: Db.dropdatabase ();
5. Clone the database from the specified host
Copy the code as follows: Db.clonedatabase ("127.0.0.1"); Cloning data from the database on the specified machine to the current database
6, copying the specified database data from the specified machine to a database The
Copy code code is as follows: Db.copydatabase ("MyDB", "temp", "127.0.0.1"), copy the native mydb data to the TEMP database
7, repair the current database
Copy code code as follows: Db.repairdatabase ();
8. View the database currently in use
Copy code as follows: Db.getname (); The
DB, DB, and GetName methods are the same, so you can query the currently used database
9, display the current DB status
Copy code code as follows: Db.stats ();
10, current DB version
Copy code as follows: Db.version ();
11. View the link machine address of the current DB
Copy the code as follows: Db.getmongo ();
Second, collection aggregation set
1. Create a Clustered collection (table)
Copy the code code as follows: Db.createcollection ("Collname", {size:20, Capped:5, max:100});//Create success will show {"OK": 1}
Determine whether the set is a fixed capacity db.collName.isCapped ();
2. Get a clustered collection with the specified name (table)
Copy the code code as follows: Db.getcollection ("account");
3. Get all the aggregation sets of the current DB
Copy the code code as follows: Db.getcollectionnames ();
4. Displays the status of all clustered indexes in the current DB
Copy the code code as follows: Db.printcollectionstats ();
Third, user-related
1. Add a user
Copy the code code as follows: Db.adduser ("name");
Db.adduser ("UserName", "pwd123", true); Add user, set password, read-only
2, database authentication, security mode
Copy the code code as follows: Db.auth ("UserName", "123123");
3. Show all current users
Copy the code code as follows: Show users;
4. Delete users
Copy the code code as follows: Db.removeuser ("UserName");
Iv. Aggregation Collection Query
1. Check all records
Copy the code code as follows: Db.userInfo.find ();
Equivalent: select* from UserInfo;
The default is 20 records per page, and you can query the next page of data with the IT iteration command when the display is not displayed. Note: Type the IT command cannot take ";"
However, you can set the size of the data displayed on each page, using dbquery.shellbatchsize= 50, which shows 50 records per page.
2. Duplicate data for a column in the current clustered collection after the query is removed
Copy the code code as follows: Db.userInfo.distinct ("name");
Will filter out the same data in name
Equivalent: Select Distict name from UserInfo;
3. Check the record of age = 22
Copy the code code as follows: Db.userInfo.find ({"Age": 22});
Equivalent to: SELECT * from userInfo where age = 22;
4. Check the records of age > 22
Copy the code code as follows: Db.userInfo.find ({age: {$gt: 22}});
Equivalent to: SELECT * from UserInfo where >22;
5. Check the records of age < 22
Copy the code code as follows: Db.userInfo.find ({age: {$lt: 22}});
Equivalent to: SELECT * from UserInfo where <22;
6. Check the records of age >= 25
Copy the code code as follows: Db.userInfo.find ({age: {$gte: 25}});
Equivalent to: SELECT * from UserInfo where age >= 25;
7. Check the records of age <= 25
Copy the code code as follows: Db.userInfo.find ({age: {$lte: 25}});
8. Query age >= 23 and age <= 26
The copy code code is as follows: Db.userInfo.find ({age: {$gte: $lte: 26}});
9. Query the data containing MONGO in name
Copy the code code as follows: Db.userInfo.find ({name:/mongo/});
Equal to percent
[Code]select * from UserInfo where name like '%mongo% ';
10, query the name in the beginning of MONGO
Copy the code code as follows: Db.userInfo.find ({name:/^mongo/});
SELECT * from UserInfo where name is like ' mongo% ';
11. Query the specified column name, age data
Copy the code code as follows: Db.userInfo.find ({}, {name:1, age:1});
Equivalent to: Select Name, age from UserInfo;
Of course name can also be used with true or false, as in the case of Ture River Name:1 effect, if False is to exclude name, display column information other than name.
12. Query the specified column name, age data, age > 25
Copy the code code as follows: Db.userInfo.find ({age: {$gt: +}}, {name:1, age:1});
Equivalent to: Select Name, age from UserInfo where age >25;
13. Sort by age
Copy the code code as follows: Ascending: Db.userInfo.find (). Sort ({age:1});
Descending: Db.userInfo.find (). Sort ({Age:-1});
14. Query name = Zhangsan, age = 22 data
Copy the code code as follows: Db.userInfo.find ({name: ' Zhangsan ', age:22});
Equivalent to: SELECT * from userInfo where name = ' Zhangsan ' and "age = ' 22 ';
15, query the first 5 data
Copy the code code as follows: Db.userInfo.find (). Limit (5);
Equivalent: selecttop 5 * from UserInfo;
16, query 10 after the data
Copy the code code as follows: Db.userInfo.find (). Skip (10);
Equivalent to: SELECT * from UserInfo where ID not in (
Selecttop * from UserInfo
);
17, query the data between 5-10
Copy the code code as follows: Db.userInfo.find (). L