Mongodb Common operation Commands Daquan _mongodb

Source: Internet
Author: User
Tags create index mongodb create database

First, the database commonly used commands
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 code code as follows: Use YOURDB; The current database is created automatically when a collection (table) is created
3, query all databases
Copy code code as follows: Show DBS;
4, delete the current use of the database
Copy code code as follows: Db.dropdatabase ();
5, clone the database from the designated host
Copy code code as follows: 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 to a database from the specified machine
Copy code code as follows: Db.copydatabase ("MyDB", "temp", "127.0.0.1"); Copy 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 code as follows: Db.getname ();
db DB and GetName methods are the same effect, you can query the currently used database
9, display the current DB status
Copy code code as follows: Db.stats ();
10, the current DB version
Copy code code as follows: Db.version ();
11, view the current DB link machine address
Copy code code as follows: Db.getmongo ();

Second, collection aggregation collection
1. Create a Clustered collection (table)
Copy 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 the specified name of the aggregation set (table)
Copy code code as follows: Db.getcollection ("account");
3, get the current db of all the aggregation set
Copy code code as follows: Db.getcollectionnames ();
4. Displays the status of all clustered indexes in the current DB
Copy code code as follows: Db.printcollectionstats ();
Third, user-related
1. Add a user
Copy code code as follows: Db.adduser ("name");
Db.adduser ("UserName", "pwd123", true); Add user, set password, read only
2. Database Authentication and Safe mode
Copy code code as follows: Db.auth ("UserName", "123123");
3. Show all current users
Copy code code as follows: Show users;
4, delete the user
Copy code code as follows: Db.removeuser ("UserName");
Aggregate collection Query
1. Check all records
Copy code code as follows: Db.userInfo.find ();
Equivalent to: select* from UserInfo;
The default display of 20 records per page, when not displayed, you can use it iterative command to query the next page of data. Note: type it command without ";"
But you can set the size of each page to display the data, with dbquery.shellbatchsize= 50, so that each page shows 50 records.
2. Duplicate data for a column in the current clustered collection after the query has been removed
Copy code code as follows: Db.userInfo.distinct ("name");
Will filter out the same data in name
Equivalent: Select Distict name from UserInfo;
3, Query age = 22 Records
Copy 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 code code as follows: Db.userInfo.find ({age: {$gt: 22}});
Equivalent to: SELECT * from UserInfo where age >22;
5. Check the records of age < 22
Copy code code as follows: Db.userInfo.find ({age: {$lt: 22}});
Equivalent to: SELECT * from UserInfo where age <22;
6. Check the records of age >= 25
Copy 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 code code as follows: Db.userInfo.find ({age: {$lte: 25}});
8. Query age >= 23 and age <= 26
Copy code code as follows: Db.userInfo.find ({age: {$gte: $lte: 26}});
9, query name contains MONGO data
Copy code code as follows: Db.userInfo.find ({name:/mongo/});
Equal to%
[Code]select * from UserInfo where name like '%mongo% ';
10, query name in the beginning of the MONGO
Copy code code as follows: Db.userInfo.find ({name:/^mongo/});
SELECT * from UserInfo where name like ' mongo% ';
11. Query Specify column name, age data
Copy code code as follows: Db.userInfo.find ({}, {name:1, age:1});
Equivalent to: Select Name, age from UserInfo;
Of course, name can also use True or false, as with ture in the case of river Name:1 effect, if False is excluding name, display column information other than name.
12. Query Specify column name, age data, age > 25
Copy 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 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 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 code code as follows: Db.userInfo.find (). Limit (5);
Equivalent to: Selecttop 5 * from UserInfo;
16, query 10 after the data
Copy code code as follows: Db.userInfo.find (). Skip (10);
Equivalent to: SELECT * from UserInfo where ID isn't in (
Selecttop from UserInfo
);
17, the query in the data between 5-10
Copy code code as follows: Db.userInfo.find (). Limit. Skip (5);
Can be used for pagination, limit is Pagesize,skip is the first few pages *pagesize
18, OR and query
Copy code code as follows: Db.userInfo.find ({$or: [{age:22}, {age:25}]});
Equivalent to: SELECT * from userInfo where age = 25;
19, query the first piece of data
Copy code code as follows: Db.userInfo.findOne ();
Equivalent to: Selecttop 1 * from UserInfo;
Db.userInfo.find (). limit (1);
20, query a result set of the number of records
Copy code code as follows: Db.userInfo.find ({age: {$gte:}}). Count ();
Equivalent to: SELECT COUNT (*) from UserInfo where age >= 20;
21, according to a column to sort
Copy code code as follows: Db.userInfo.find ({sex: {$exists: true}}). Count ();
Equivalent to: SELECT COUNT (Sex) from userInfo;
Five, index
1. CREATE index
Copy code code as follows: Db.userInfo.ensureIndex ({name:1});
Db.userInfo.ensureIndex ({name:1, TS:-1});
2. Querying all indexes of the current aggregation collection
Copy code code as follows: Db.userInfo.getIndexes ();
3. View the total index record size
Copy code code as follows: Db.userInfo.totalIndexSize ();
4. Read all index information for the current collection
Copy code code as follows: Db.users.reIndex ();
5, delete the specified index

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.