MongoDB Beginner-Common basic commands

Source: Internet
Author: User
Tags create index mongodb create database
1, MongoDB official website download: https://www.mongodb.org/downloads can download the installation version and press release; temporarily select Unzip installation 2, cmd into the MongoDB installation directory under the bin, start MongoDB. Mongod Open the command while using--dbpath to specify the data storage location as "DB" folder. Mongod--dbpath=e:\study\db mongod--dbpath= E:\study\db--logpath= E:\study\db\ MongoDB.log--auth--port=27017--fork 3 , basic additions and deletions (1), shell operation input MONGO after performing the following actions
Show DBS; display a list of databases
Show collections; Displays a collection in the current database (similar to a table in a relational database) show users; show users; use myfirstdb; toggle \ Create Database
Db.help (); Database Common commands
Db.clonedatabase ("127.0.0.1"); Clones data from a database on a specified machine to the current database
Db.copydatabase ("MyDB", "temp", "127.0.0.1"), copy the native mydb data to the TEMP database
Collection Aggregation collection Operations
1. Create a Clustered collection (table)
Db.createcollection ("Collname", {size:20, capped:true, max:100}); #当为true时不能删除, or you can delete
2. Get a clustered collection with the specified name (table)
Db.getcollection ("account");
Db.getcollectionnames ();
3. Displays the status of all clustered indexes in the current DB
Db.printcollectionstats ();
User-related
1. Add a user
Db.createuser
Example: DB. CreateUser ({User: "Accountuser", pwd: "Password", roles: ["ReadWrite", "DbAdmin"]}) 2, delete user Db.dropuser ("UserName");
Detailed operation of aggregations (tables similar to relational databases) check: Db.dong.find (); #类似 select * from Dong; But you can set the size of the data displayed per page, using dbquery. shellbatchsize = 50; This results in 50 records per page. Default 20 with it iteration: Db.dong.save ({name: "Dong", age:26}); Add Data modification: db.dong.update ({age:26},{$set: {name: ' Zhou '}}); Similar: update Dong Set name = ' Zhou ' where age = 26; Delete: Db.dong.remove ({age:26}); Delete Age is 26 data (Db.dong. iscapped () True at that time cannot delete) for (Var i=0;i<20;i++) {Db.dong.save ({name: "Dong" +i,age:26+i})};
Some commonly used query commands: 2. Duplicate data db.userInfo.distinct ("name") of a column in the current clustered collection after the query is removed; Will filter out the same data in name equivalent to: Select Distict name from UserInfo; 3, Query age = 22 of the record Db.userInfo.find ({"Age": 22}); Equivalent to: SELECT * from userInfo where age = 22; 4. Check the age > 22 record Db.userInfo.find ({age: {$gt: 22}}); Equivalent to: SELECT * from UserInfo where >22; 5. Check the age < 22 record Db.userInfo.find ({age: {$lt: 22}}); Equivalent to: SELECT * from UserInfo where <22; 6, Query the age >= 25 record Db.userInfo.find ({age: {$gte: 25}}); Equivalent to: SELECT * from UserInfo where age >= 25; 7, Query the age <= 25 record Db.userInfo.find ({age: {$lte: 25}}); 8, query Age >= 23 and age <=-Db.userInfo.find ({age: {$gte: $, $lte: 26}}); 9, query name contains MONGO data Db.userInfo.find ({name:/mongo/}); Equivalent to a percent of select * from UserInfo where the name like '%mongo% '; 10. Query name Db.userInfo.find ({name:/^mongo/}) beginning with 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 when using ture in case of River NAMe:1 effect, if you use False to exclude name, display column information other than name. 12. Query the specified column name, age data, age > Db.userInfo.find ({age: {$gt: +}}, {name:1, age:1}); Equivalent to: Select Name, age from UserInfo where age >25; 13. Sort Ascending by Age: 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 (5). Can be used for pagination, limit is Pagesize,skip is the number of pages *pagesize 18, or with query Db.userInfo.find ({$or: [{age:22}, {age:25}]}); Equivalent to: SELECT * from UserInfo where is age = 25 19, query the first 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. Count Db.userInfo.find ({sex: {$exists: true}}) in a column. Equivalent to: SELECT COUNT (Sex) from userInfo; Index 1, CREATE index Db.userInfo.ensureIndex ({name:1}); Db.userInfo.ensureIndex ({name:1, TS:-1}); 2, query the current aggregate collection all Indexes db.userInfo.getIndexes (); 3, view the total index record size db.userInfo.totalIndexSize (); 4, read all index information of the current collection Db.users.reIndex ();
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.