MongoDB Syntax Usage Summary _mongodb

Source: Internet
Author: User
Tags create index findone mkdir mongodb prev
The data structure he supports is very loose and is a JSON-like Bjson format, so you can store more complex data types. The biggest feature of MONGO is that the query language he supports is very powerful, and its syntax is somewhat similar to an object-oriented query language, which can almost achieve most of the functions of a single table query like relational database, and also supports indexing of data.
It is characterized by high-performance, easy to deploy, easy to use, storage data is very convenient.

1. Acquisition and installation of MongoDB

(1) Get the address http://www.mongodb.org/downloads according to their own needs to choose the appropriate version, Linux can use the wget command.
(2) Decompression mongodb-win32-i386-1.8.1
(3) Create the data store folder, MongoDB the default data directory/data/db
C:/> Mkdir/data
C:/> mkdir/data/db
(4) Running MongoDB
Mongod.exe-server side of the database, equivalent to MySQL's mysqld command, start server side
Mongo.exe-Database client, equivalent to MySQL mysql command, open Admin Console

Start a service
Mongod.exe--dbpath f:/database/mongodb/db/
--dbpath Data File storage path
--port Data Service Port
C:/> Cd/my_mongo_dir/bin
C:/my_mongo_dir/bin > Mongod/Start mongod server, default database path/data/db, Port 27071
Start the client
Mongo.exe Cclove
The name of the database to which Cclove is connected
C:/> Cd/my_mongo_dir/bin
C:/my_mongo_dir/bin> MONGO

2. Familiar with MongoDB data manipulation statements, class SQL

Database operation syntax
MONGO--path
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.cname
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 connection object
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 () What grade?
Db.getreplicationinfo () What's the message?
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

DataSet (table) Operation syntax
Db.linlin.find ({id:10}) returns the DataSet id=10 the Linlin dataset
Db.linlin.find ({id:10}). Count () returns the total number of data Linlin the dataset id=10
Db.linlin.find ({id:10}). Limit (2) returns a DataSet from the second set of Datasets id=10 by the Linlin dataset
Db.linlin.find ({id:10}). Skip (8) Returns a dataset id=10 a dataset from 0 to eighth in a dataset of Linlin datasets
Db.linlin.find ({id:10}). Limit (2). Skip (8) returns data from article II to eighth of the dataset id=1= data set Linlin
Db.linlin.find ({id:10}). Sort () returns the sorted data set id=10 the Linlin dataset
Db.linlin.findOne ([query]) returns one piece of data that matches the criteria
Db.linlin.getDB () returns the name of the database to which this dataset belongs
Db.linlin.getIndexes () returns index information for some datasets
Db.linlin.group ({key:...,initial:...,reduce: ...) [, cond: ...]})
Db.linlin.mapReduce (mayfunction,reducefunction,<optional params>)
Db.linlin.remove (query) deletes a piece of data in a dataset
Db.linlin.renameCollection (newName) rename some dataset names
Db.linlin.save (obj) inserts a piece of data into the dataset
Db.linlin.stats () Returns the state of this dataset
Db.linlin.storageSize () returns the storage size of this dataset
Db.linlin.totalIndexSize () returns the index file size of this dataset
Db.linlin.totalSize () returns the total size of some datasets
Db.linlin.update (Query,object[,upsert_bool]) update a piece of data in this dataset
Db.linlin.validate () validates this dataset
Db.linlin.getShardVersion () returns the dataset share version number

Db.linlin.find ({' name ': ' Foobar '}) SELECT * from Linlin where name= ' Foobar '
Db.linlin.find () SELECT * FROM Linlin
Db.linlin.find ({' ID ': Ten} '). Count () Select COUNT (*) from Linlin where id=10
Db.linlin.find (). Limit (20) read 20 data from tenth of query results select * FROM Linlin limit 10,20----------MySQL
Db.linlin.find ({' ID ': {$in: [25,35,45]}}) select * from Linlin where ID in (25,35,45)
Db.linlin.find (). Sort ({' ID ': -1}) SELECT * from Linlin ORDER BY ID Desc
Db.linlin.distinct (' name ', {' ID ': {$lt:}}) select DISTINCT (name) from Linlin where id<20

Db.linlin.group ({key:{' name ': true},cond:{' name ': ' foo '},reduce:function (obj,prev) {prev.msum+=obj.marks;}, INITIAL:{MSUM:0}})
Select Name,sum (Marks) from Linlin GROUP by name
Db.linlin.find (' this.id<20 ', {name:1}) select name from Linlin where id<20

Db.linlin.insert ({' name ': ' Foobar ', ' Age ':}) inserts into the Linlin (' name ', ' age ') VALUES (' Foobar ', 25)
Db.linlin.insert ({' name ': ' Foobar ', ' age ':, ' email ': ' cclove2@163.com '})

Db.linlin.remove ({}) Delete * from Linlin
Db.linlin.remove ({' Age ':}) Delete Linlin where age=20
Db.linlin.remove ({' age ': {$lt:}}) Delete Linlin where age<20
Db.linlin.remove ({' age ': {$lte:}}) Delete Linlin where age<=20
Db.linlin.remove ({' age ': {$gt:}}) Delete Linlin where age>20
Db.linlin.remove ({' age ': {$gte:}}) Delete Linlin where age>=20
Db.linlin.remove ({' age ': {$ne:}}) Delete Linlin where age!=20

Db.linlin.update ({' name ': ' Foobar '},{$set: {' Age ':}}) update Linlin set age=36 where name= ' Foobar '
Db.linlin.update ({' name ': ' Foobar '},{$inc: {' Age ': 3}}) update Linlin set age=age+3 where name= ' Foobar '

The official Operation statement table:

Uplink: SQL Action Statement
Downlink: Mongo Action Statement
CREATE TABLE USERS (a number, b number)
Db.createcollection ("Mycoll")

INSERT into USERS VALUES (1,1)
Db.users.insert ({a:1,b:1})

SELECT A,b from users
Db.users.find ({}, {a:1,b:1})

SELECT * from users
Db.users.find ()

SELECT * from Users WHERE age=33
Db.users.find ({age:33})

SELECT a,b from users WHERE age=33
Db.users.find ({age:33}, {a:1,b:1})

SELECT * from Users WHERE age=33 order by name
Db.users.find ({age:33}). Sort ({name:1})

SELECT * from Users WHERE age>33
Db.users.find ({' age ': {$gt: 33}}})

SELECT * from Users WHERE age<33
Db.users.find ({' age ': {$lt: 33}}})

SELECT * from the users WHERE name like "%joe%"
Db.users.find ({name:/joe/})

SELECT * from the users WHERE name like "joe%"
Db.users.find ({name:/^joe/})

SELECT * from Users WHERE age>33 and age<=40
Db.users.find ({' age ': {$gt: $lte: 40}})})

SELECT * from users ' ORDER by name DESC
Db.users.find (). Sort ({name:-1})

SELECT * from Users WHERE a=1 and b= ' Q '
Db.users.find ({a:1,b: ' Q '})

SELECT * from users LIMIT SKIP 20
Db.users.find (). Limit (a). Skip (20)

SELECT * from users WHERE a=1 or b=2
Db.users.find ({$or: [{a:1}, {b:2}]})

SELECT * from users LIMIT 1
Db.users.findOne ()

SELECT DISTINCT last_name from users
Db.users.distinct (' last_name ')

SELECT COUNT (*y) from users
Db.users.count ()

SELECT COUNT (*y) from the users where age > 30
Db.users.find ({age: {' $gt ':}}). Count ()

SELECT COUNT (age) from users
Db.users.find ({age: {' $exists ': true}}). Count ()

CREATE INDEX myindexname on users (name)
Db.users.ensureIndex ({name:1})

CREATE INDEX myindexname on users (Name,ts DESC)
Db.users.ensureIndex ({name:1,ts:-1})

EXPLAIN SELECT * from users WHERE z=3
Db.users.find ({Z:3}). Explain ()

UPDATE users SET a=1 WHERE b= ' q '
Db.users.update ({b: ' Q '}, {$set: {a:1}}, False, True)

UPDATE users SET a=a+2 WHERE b= ' q '
Db.users.update ({b: ' Q '}, {$inc: {a:2}}, False, True)

DELETE from users WHERE z= "ABC"
Db.users.remove ({z: ' abc '});
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.