Common commands and syntaxes for MongoDB

Source: Internet
Author: User

5. More commands

DB. adduser (username, password) Add User

DB. Auth (usrename, password) sets database connection Verification

DB. clonedatabase (fromhost) clone a database from the target server
DB. commandhelp (name) returns the help for the command
DB. copydatabase (fromdb, todb, fromhost) copy the database fromdb --- source database name, todb --- target database name, fromhost --- source database server address
DB. createcollection (name, {size: 3333, capped: 333, Max: 88888}) to create a dataset, equivalent to a table
DB. currentop () cancels the current operation of the current database
DB. dropdatabase () Delete the current database
DB. eval (func, argS) run code server-side
DB. getcollection (cname) gets a data set. In the same usage: DB ['cname'] or
DB. getcollenctionnames () Get the name list of all data sets
DB. getlasterror () returns the message indicating the last error.
DB. getlasterrorobj () returns the last error object
DB. getmongo () gets the connection object of the current server 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 database to be operated on.
DB. getpreverror () returns the previous error object
DB. getprofilinglevel ()
DB. getreplicationinfo () to obtain duplicate data
DB. getsisterdb (name) Get the dB at the same server as this onew
DB. Killop () Stop (kill) the current operation in the current database
DB. printcollectionstats () returns the dataset status of the current database
DB. printreplicationinfo ()
DB. printslavereplicationinfo ()
DB. printshardingstatus () returns whether the current database is a shared database
DB. removeuser (username) delete a user
DB. repairdatabase () repairs 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 () Close the current service program
DB. Version () returns the version information of the current program.

 

DB. Test. Find ({ID: 10}) returns the test dataset id = 10
DB. Test. Find ({ID: 10}). Count () returns the total number of data in the test dataset id = 10.
DB. Test. Find ({ID: 10}). Limit (2) returns the dataset whose test dataset id = 10 starts from the second one.
DB. Test. Find ({ID: 10}). Skip (8) returns the data set from 0 to the eighth in the test dataset id = 10.
DB. Test. Find ({ID: 10}). Limit (2). Skip (8) returns the data from the second to the eighth in the test dataset id = 1 =.
DB. Test. Find ({ID: 10}). Sort () returns the sorting dataset of the test dataset id = 10.
DB. Test. findone ([query]) returns a data record that meets the condition.
DB. Test. getdb () returns the name of the database to which the dataset belongs.
DB. Test. getindexes () returns index information for some datasets.
DB. Test. Group ({key:..., initial:..., reduce:... [, Cond:...]})
DB. Test. mapreduce (mayfunction, performancefunction, <optional Params>)
DB. Test. Remove (query) deletes a piece of data in the dataset.
DB. Test. renamecollection (newname) rename some dataset names
DB. Test. Save (OBJ) inserts a data entry into the dataset.
DB. Test. Stats () returns the status of this dataset.
DB. Test. storagesize () returns the storage size of this dataset.
DB. Test. totalindexsize () returns the size of the index file for this dataset.
DB. Test. totalsize () returns the total size of some datasets.
DB. Test. Update (query, object [, upsert_bool]) updates a piece of data in this dataset.
DB. Test. Validate () verify this dataset
DB. Test. getshardversion () returns the shared version of the dataset.

 

Vi. Comparison between MongoDB syntax and SQL Syntax of existing relational databases

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': 10}). Count () <=> select count (*) from test where id = 10
DB. Test. Find (). Skip (10). Limit (20) <=> 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: 20 }}) <==> 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': 25}) <=> insert into test ('name', 'age') values ('foobar ', 25)
DB. Test. Remove ({}) <=> Delete * from test
DB. Test. Remove ({'age': 20}) <=> delete test where age = 20
DB. Test. Remove ({'age': {$ LT: 20 }}) <=> elete test where age <20
DB. Test. Remove ({'age': {$ LTE: 20 }}) <==> delete test where age <= 20
DB. Test. Remove ({'age': {$ GT: 20 }}) <=> delete test where age> 20
DB. Test. Remove ({'age': {$ GTE: 20 }}) <=> delete test where age> = 20
DB. Test. Remove ({'age': {$ ne: 20 }}) <=> delete test where age! = 20
DB. test. update ({'name': 'foobar'}, {$ set: {'age': 36 }}) <==> 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'

Reprinted: http://www.cnblogs.com/xumingxiang/archive/2012/04/08/2437468.html

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.