MongoDB basic Commands

Source: Internet
Author: User
Tags create index tojson

The project used to MongoDB, the basic is to use Robomongo to connect the server, do some of the lower permissions to delete and change the operation. Commands often forget to record some of the most frequently used commands for collections for easy querying:

15}); 2 a Clustered collection (table) with the specified name Db.getcollection ("account"); 3 , get all the aggregation sets of the current DB Db.getcollectionnames (); 4 , showing the status of all clustered indexes of the current DB Db.printcollectionstats ();

Query for the collection:

1, querying All Records db.userInfo.find (); equivalent to:Select* fromUserInfo; 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 ";" but you can set the size of the data displayed per page, using Dbquery.shellbatchsize= -This will show 50 records per page. 2, the duplicate data db.userInfo.distinct for a column in the current clustered collection after the query is removed ("name"), filtering out the same data in name is equivalent to:SelectDistict Name fromUserInfo;3, Query age =22 of Records Db.userInfo.find ({" Age": A}); equivalent to:Select* fromUserInfowhereAge = A; 4, Query Age >22 of Records Db.userInfo.find ({age: {$gt: A}}); equivalent to:Select* fromUserInfowhereAge > A; 5, Query Age <22 of Records Db.userInfo.find ({age: {$lt: A}}); equivalent to:Select* fromUserInfowhereAge < A; 6, Query Age >=25 of Records Db.userInfo.find ({age: {$gte: -}}); equivalent to:Select* fromUserInfowhereAge >= -; 7, Query Age <=25 of Records Db.userInfo.find ({age: {$lte: -}}); 8, Query Age >= atand age <= -Db.userInfo.find ({age: {$gte: at, $lte: -}}); 9, the query name contains the MONGO data Db.userInfo.find ({name:/mongo/});//equal to percentSelect* fromUserInfowhereName like '%mongo%';Ten, Query the name Db.userInfo.find ({name: MONGO) that begins with/^mongo/});Select* fromUserInfowhereName like ' mongo%'; One, query the specified column name, age data Db.userInfo.find ({}, {name:1, Age:1}); equivalent to:SelectName, age fromUserInfo; 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.  A, query the specified column name, age data, age > -Db.userInfo.find ({age: {$gt: -}}, {name:1, Age:1}); equivalent to:SelectName, age fromUserInfowhereAge > -;  -, sorted by age Ascending: Db.userInfo.find (). Sort ({ages:1}); Descending: Db.userInfo.find (). Sort ({age:-1});  -, Query name = Zhangsan, age =22 of Data Db.userInfo.find ({name:'Zhangsan', Age: A}); equivalent to:Select* fromUserInfowherename = ' Zhangsan ' and age = ' A'; the, query the first 5 data db.userInfo.find (). Limit (5); equivalent: Selecttop5* fromUserInfo; -, query the data after 10 Db.userInfo.find (). Skip (Ten); equivalent to:Select* fromUserInfowhereID notinch(SelecttopTen* fromuserInfo);  -, query in 5-data between 10 db.userInfo.find (). Limit (Ten). Skip (5); can be used for pagination, limit is Pagesize,skip is the first page*pageSize -, OR and query Db.userInfo.find ({$or: [{Age: A}, {age: -}]}); equivalent to:Select* fromUserInfowhereAge = Aor age = -;  +, query the first Data Db.userInfo.findOne (); equivalent to: Selecttop1* fromuserinfo;db.userinfo.find (). Limit (1);  -, query the number of record bars for a result set Db.userInfo.find ({age: {$gte: -}}). count (); equivalent to:SelectCOUNT (*) fromUserInfowhereAge >= -;  +, sort by a column db.userInfo.find ({sex: {$exists:true}}). count (); equivalent to:SelectCOUNT (Sex) fromUserInfo;

Index operations:

1, CREATE index Db.userInfo.ensureIndex ({name:1});d B.userinfo.ensureindex ({name:1, TS:-1}); 2, queries the current collection of 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 the specified index Db.users.dropIndex ("Name_1"); 6, delete all index indexes db.users.dropIndexes ();

Change and delete:

1, add Db.users.save ({name: ' Zhangsan ', Age: -, Sex:true}); The data column of the added data is not fixed, depending on the data added2, modify Db.users.update ({age: -}, {$Set: {Name:'ChangeName'}},false,true); equivalent to: Update usersSetname = ' ChangeName 'whereAge = -; Db.users.update ({name:'Lisi'}, {$inc: {age: -}},false,true); equivalent to: Update usersSetAge = Age + - whereName =' Lisi '; Db.users.update ({name:'Lisi'}, {$inc: {age: -}, $Set: {Name:'HoHo'}},false,true); equivalent to: Update usersSetAge = Age + -, name = ' HoHo 'whereName =' Lisi ';3, delete db.users.remove ({age: the}); 4, Query modification Delete db.users.findAndModify ({query: {age: {$gte: -}}, Sort: {age:-1}, Update: {$Set: {Name:'A2'}, $inc: {age:2}}, remove:true});

    
    
    Update: {$set: {name: ' A2 '}, $inc: {age:2}},
    Remove:true
});


If you are interested, try the action of the statement block:

1, simple Hello Worldprint ("Hello world!"This method calls the print function, and writes directly to the"Hello world!"the effect is the same;2, converting an object to Jsontojson (NewObject ()); Tojson (NewObject ('a')); 3, loop Add data> for(vari =0; I < -; i++) {... db.users.save ({name:"u_"+ I, Age: A+ I, sex:i%2});... }; This adds 30 data to the loop, and you can also omit the notation> for(vari =0; I < -; i++) Db.users.save ({name:"u_"+ I, Age: A+ I, sex:i%2}); You can use it to view the next page of information when you use Db.users.find () query to display multiple data instead of one page display.4, find cursor query>varcursor =Db.users.find ();> while(Cursor.hasnext ()) {Printjson (Cursor.next ());} This allows you to query all the users information, as well as to writevarcursor =Db.users.find (); while(Cursor.hasnext ()) {Printjson (cursor.next);} You can also omit the {} number5, foreach iteration Loop Db.users.find (). foreach (Printjson); a function must be passed in foreach to process data information for each iteration6, the Find cursor is treated as an arrayvarcursor =Db.users.find (); cursor[4]; To get the index of subscript 4 that data can be treated as an array, then you can get its length: cursor.length (); or cursor.count (); So we can also display the data in a loop for(vari =0, Len = C.length (); i < Len; i++) Printjson (C[i]); 7, convert the Find cursor to an array>vararr =Db.users.find (). ToArray ();> Printjson (arr[2]); convert it to an array using the ToArray method8, customizing our own query results to show only age<=28 and only shows age this column of data Db.users.find ({age: {$lte: -}}, {age:1}). ForEach (Printjson);d B.users.find ({age: {$lte: -}}, {age:true}). ForEach (Printjson); Excludes the Age column Db.users.find ({age: {$lte: -}}, {age:false}). ForEach (Printjson); 9, the foreach transfer function displays information Db.things.find ({x:4}). ForEach (function (x) {print (Tojson (x));});

MongoDB basic Commands

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.