MongoDB database Common SQL commands detailed

Source: Internet
Author: User
Tags auth file size mongodb prev

The use testdb//using command toggles the current database, and if the database does not exist, a new one is created first.

Db.users.insert ({' name ': ' Xumingxiang ', ' sex ': ' Man '})//This command inserts a piece of data into the Users collection. If the collection users do not exist, a new one is created before the data is inserted, and the parameters are passed in JSON format.

Show DBS//Display all databases

Show Collections//Display all collections under the current database

Db.users.find ()//Display all data documents under the Users collection

Db.users.update ({' name ': ' Xiangshu '},{' $set ': {' sex ': ' Women '}},upsert=true,multi=false)

Upsert: Insert if not present

Multi: Allow multiple records to be modified

Db. Users.remove ({' name ': ' Xumingxiang '})//delete a piece of data

Db.users.remove ()//delete all records

Db.users.drop ()///delete collection

Db.dropdatabase ()//delete the current database

Use admin//switch to create user

Db.adduser ("UserName", "PWD")//create User

Db.auth ("UserName", "PWD")//set user to allow connections

Db.createcollection ("tablename")//CREATE TABLE

Use Password to access

Use admin

Db.adduser ("admin", "admin")//here must set the password, otherwise the test database settings password is not valid

Db.auth ("admin", "admin")

Use test

Db.adduser ("Test", "test")

============================= Other Commands ================================

Db. AddUser (username,password) Add user

Db.auth (Usrename,password) Set up database connection verification

Db.clonedatabase (fromhost) clones a database from the target server

DB.COMMANDHELP (name) returns the Help for the command

Db.copydatabase (fromdb,todb,fromhost) Copy database fromdb-source database name, todb-target database name, fromhost-source database server address

Db.createcollection (name,{size:3333,capped:333,max:88888}) creates a dataset that is equivalent to a table

Db.currentop () cancels the current operation of the current library

Db.dropdatabase () deletes the current database

Db.eval (Func,args) Run code server-side

Db.getcollection (CNAME) obtains a data collection, same usage: db[' cname '] or

Db.getcollenctionnames () Gets the list of names for all data sets

Db.getlasterror () returns a prompt message for the last error

Db.getlasterrorobj () Returns the object of the last error

Db.getmongo () Gets the current server's connection object get the server

Db.getmondo (). Setslaveok () Allow this connection to read from then Nonmaster membr of a replica pair

Db.getname () returns the name of the operation database

Db.getpreverror () returns the previous Error object

Db.getprofilinglevel ()

Db.getreplicationinfo () to get duplicate data

DB.GETSISTERDB (name) get the DB in the same server as this onew

Db.killop () Stop (kill) The current operation in the current library

Db.printcollectionstats () returns the dataset state of the current library

Db.printreplicationinfo ()

Db.printslavereplicationinfo ()

Db.printshardingstatus () returns whether the current database is a shared database

Db.removeuser (username) Delete User

Db.repairdatabase () repair the current database

Db.reseterror ()

Db.runcommand (cmdobj) run a database command. If Cmdobj is a string, turns it into {cmdobj:1}

Db.setprofilinglevel (level) 0=off,1=slow,2=all

Db.shutdownserver () Closes the current service program

Db.version () returns the version information of the current program

Db.test.find ({id:10}) returns the DataSet id=10 the test dataset

Db.test.find ({id:10}). Count () returns the total number of data id=10 the test dataset

Db.test.find ({id:10}). Limit (2) returns the dataset that the test dataset id=10 starts with the second data set

Db.test.find ({id:10}). Skip (8) returns a dataset from 0 to eighth of the DataSet id=10 the test dataset

Db.test.find ({id:10}). Limit (2). Skip (8) returns data from the second to eighth data set id=1= the test dataset

Db.test.find ({id:10}). Sort () returns the sorted dataset id=10 the test dataset

Db.test.findOne ([query]) returns one piece of data that matches the criteria

Db.test.getDB () returns the name of the database to which this dataset belongs

Db.test.getIndexes () returns index information for some datasets

Db.test.group ({key:...,initial:...,reduce: ...) [, cond: ...]})

Db.test.mapReduce (mayfunction,reducefunction,<optional params>)

Db.test.remove (query) deletes a piece of data in a dataset

Db.test.renameCollection (newName) rename some dataset names

Db.test.save (obj) inserts a piece of data into the dataset

Db.test.stats () Returns the state of this dataset

Db.test.storageSize () returns the storage size of this dataset

Db.test.totalIndexSize () returns the index file size of this dataset

Db.test.totalSize () returns the total size of some datasets

Db.test.update (Query,object[,upsert_bool]) update a piece of data in this dataset

Db.test.validate () validates this dataset

Db.test.getShardVersion () returns the dataset share version number

Comparison of MongoDB syntax with existing relational database SQL syntax

MongoDB syntax MySQL syntax

Db.test.find ({' name ': ' Foobar '}) <==> select * from test where name= ' Foobar '

Db.test.find () <==> select * FROM Test

Db.test.find ({' ID ': Ten} '). Count () <==> Select COUNT (*) from Test where id=10

Db.test.find (). Skip (limit) <==> select * FROM Test limit 10,20

Db.test.find ({' ID ': {$in: [25,35,45]}}) <==> select * from Test where ID in (25,35,45)

Db.test.find (). Sort ({' ID ': -1} ') <==> select * FROM Test ORDER BY ID Desc

Db.test.distinct (' name ', {' ID ': {$lt:}}) <==> select DISTINCT (name) from Test where id<20

Db.test.group ({key:{' name ': true},cond:{' name ': ' foo '},reduce:function (obj,prev) {prev.msum+=obj.marks;},initial:  {msum:0}}) <==> Select Name,sum (marks) from test group by name

Db.test.find (' this.id<20′,{name:1}) <==> select name from Test where id<20

Db.test.insert ({' name ': ' Foobar ', ' Age ':}) <==>insert to test (' name ', ' age ') VALUES (' Foobar ', 25)

Db.test.remove ({}) <==> Delete * from test

Db.test.remove ({' Age ':}) <==> Delete test where age=20

Db.test.remove ({' age ': {$lt:}}) <==> elete test where age<20

Db.test.remove ({' age ': {$lte:}}) <==> delete test where age<=20

Db.test.remove ({' age ': {$gt:}}) <==> delete test where age>20

Db.test.remove ({' age ': {$gte:}}) <==> delete test where age>=20

Db.test.remove ({' age ': {$ne:}}) <==> delete test where age!=20

Db.test.update ({' name ': ' Foobar '},{$set: {' Age ':}}) <==> update test set age=36 where Name= ' Foobar '

Db.test.update ({' name ': ' Foobar '},{$inc: {' Age ': 3}}}) <==> update test set age=age+3 where Name= ' Foobar '

Note that the above command case sensitivity

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.