MongoDB Basic Commands Collation

Source: Internet
Author: User
Tags create index

recently in MU class online to see the next Mongdb video, here to summarize and tidy up
MongoDB Learning site:
1. MongoDB Website: www.mongodb.org
2. MongoDB Domestic official website: www.monging.com
3. Chinese MongoDB document address: docs.monging.com
4, MongoDB's github:https://github.com/mongodb/
5, MongoDB's jira:https://jira.mongodb.org/

6, two Google Groups:mongdb-user and MONGO-CN

Why Choose MongoDB

1. No data structure restrictions

(1) Without the concept of a table structure, each record can have a completely different structure

(2) Easy and quick business development

(3) SQL database requires pre-defined table structure reuse

2. Full index support

(1) Key-value of Redis

(2) HBase single index, level two index needs to be implemented by itself

3. Convenient redundancy and expansion

(1) Replication set ensures data security

(2) Expansion of data size by sharding

The basic operation of MongoDB:

1. Using the database

Show DBS: Display Database list

Show Collections: Displays a collection in the current database (similar to a table in a relational database)

Show Users: Show user

2. Switch/CREATE Database
Use Yourdb; The current database is created automatically when a collection (table) is created
3. Query all databases
Show DBS;
4. Delete the currently used database
Db.dropdatabase ();
5. Cloning a database from a specified host
Db.clonedatabase ("127.0.0.1"); Clones data from a database on a 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 native mydb data to the TEMP database
7. Repairing the current database
Db.repairdatabase ();
8. View the database currently in use
Db.getname ();
db The DB and GetName methods are the same effect and can query the currently used database
9. Display the current DB status
Db.stats ();
10. Current DB version
Db.version ();
11. View the current DB link machine address
Db.getmongo ();
User-related
1. Add a user
Db.adduser ("name");
Db.adduser ("UserName", "pwd123", true); Add user, set password, read-only
2, database authentication, security mode
Db.auth ("UserName", "123123");
3. Show all current users
Show Users;
4. Delete users
Db.removeuser ("UserName");
Other
1, the error message before the query
Db.getpreverror ();
2. Clear the Error Record
Db.reseterror ();

1. Check all records
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
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
Db.userInfo.find ({"Age": 22});
Equivalent to: SELECT * from userInfo where age = 22;

4. Check the records of age > 22
Db.userInfo.find ({age: {$gt: 22}});
Equivalent to: SELECT * from UserInfo where >22;

5. Check the records of age < 22
Db.userInfo.find ({age: {$lt: 22}});
Equivalent to: SELECT * from UserInfo where <22;

6. Check the records of age >= 25
Db.userInfo.find ({age: {$gte: 25}});
Equivalent to: SELECT * from UserInfo where age >= 25;

7. Check the records of age <= 25
Db.userInfo.find ({age: {$lte: 25}});

8. Query age >= 23 and age <= 26
Db.userInfo.find ({age: {$gte: $lte: 26}});

9. Query the data containing MONGO in name
Db.userInfo.find ({name:/mongo/});
Equal to percent
SELECT * from UserInfo where name is like '%mongo% ';

10, query the name in the beginning of MONGO
Db.userInfo.find ({name:/^mongo/});
SELECT * from UserInfo where name is like ' mongo% ';

11. Query the specified column name, age data
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
Db.userInfo.find ({age: {$gt: +}}, {name:1, age:1});
Equivalent to: Select Name, age from UserInfo where age >25;

13. Sort by age
Ascending: Db.userInfo.find (). Sort ({age:1});
Descending: Db.userInfo.find (). Sort ({Age:-1});

14. Query name = Zhangsan, age = 22 data
Db.userInfo.find ({name: ' Zhangsan ', age:22});
Equivalent to: SELECT * from userInfo where name = ' Zhangsan ' and "age = ' 22 ';

15, query the first 5 data
Db.userInfo.find (). Limit (5);
Equivalent: selecttop 5 * from UserInfo;

16, query 10 after the data
Db.userInfo.find (). Skip (10);
Equivalent to: SELECT * from UserInfo where ID not in (
Selecttop * from UserInfo
);

17, query the data between 5-10
Db.userInfo.find (). Limit (Ten). Skip (5);
Can be used for pagination, limit is Pagesize,skip is the first few pages *pagesize

18, OR and query
Db.userInfo.find ({$or: [{age:22}, {age:25}]});
Equivalent to: SELECT * from UserInfo where is age = 25

19. Query the first piece of data
Db.userInfo.findOne ();
Equivalent: Selecttop 1 * from UserInfo;
Db.userInfo.find (). limit (1);

20. Query the number of record bars for a result set
Db.userInfo.find ({age: {$gte: +}}). Count ();
Equivalent to: SELECT COUNT (*) from UserInfo where age >= 20;

21. Sort by a column
Db.userInfo.find ({sex: {$exists: true}}). Count ();
Equivalent to: SELECT COUNT (Sex) from userInfo;


Indexes (indexes are created and reused before they can cause efficiency problems)

1, CREATE INDEX
Span style= "White-space:pre" > Db.userInfo.ensureIndex ({name:1});
Db.userInfo.ensureIndex ({name:1, TS:-1});
 
2, querying the current clustered collection all indexes
Db.userInfo.getIndexes ();
 
3, view total index record size
Db.userInfo.totalIndexSize ();
 
4, reading all index information for the current collection
Db.users.reIndex ();
 
5, delete specified index
Db.users.dropIndex ("Name_1");
 
6, delete all indexed indexes
Db.users.dropIndexes ();
  modify, add, and delete collection data

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

MongoDB Basic Commands Collation

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.