The MongoDB syntax USES a summary

  • 2020-05-06 11:57:45
  • OfStack

The data structure it supports is very loose and is in bjson format similar to json, so it can store more complex data types. The biggest feature of Mongo is that the query language it supports is very powerful. Its syntax is somewhat similar to that of an object-oriented query language, and it can almost realize most of the functions of a relational database single table query, and it also supports data indexing.
It is characterized by high performance, easy to deploy, easy to use, storage of data is very convenient.

1. MongoDB get and install

Access the address http
(1) : / / www mongodb. org/downloads according to their own need to choose the corresponding version, can use under linux wget command.
(2) unzip mongodb-win32-i386-1.8.1
(3) create data storage folder, mongodb default data directory /data/db
C:/ > mkdir /data
C:/ > mkdir /data/db
(4) run MongoDB
Es39en.exe - the server side of the database, equivalent to the mysqld command of mysql, starts
on the server side Es44en.exe - the client of the database, equivalent to mysql command of mysql, opens the administrative console

Launch 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 database name
is connected to by cclove C:/ > cd /my_mongo_dir/bin
C:/my_mongo_dir/bin > mongo

Familiar with 2. Familiar with MongoDB data operation statements, sql

database operation syntax
mongo --path
Es94en.AddUser (username,password) add user
Es99en.auth (usrename,password) sets up database connection validation
Es104en.cloneDataBase (fromhost) clones a database
from the target server db.commandHelp(name) returns the help for the command
Es110en.copyDatabase (fromdb,todb,fromhost) copy database fromdb-- source database name, todb-- target database name, fromhost-- source database server address
db. createCollection (name, {capped size: 3333:333, max: 88888}) create a data set, the equivalent of a table
Es126en.currentOp () cancels the current operation of the current library,
Es129en.dropDataBase () deletes the current database
db.eval(func,args) run code server-side
Es134en.getCollection (cname) gets a data set, db['cname'] or db.cname
Es143en.getCollenctionNames () gets a list of names for all data sets
Es146en.getLastError () returns the last error message,
Es149en.getLastErrorObj () returns the last wrong object
Es152en.getMongo () gets the connection object get the server connection object
for the current server db.getMondo().setSlaveOk() allow this connection to read from then nonmaster membr of a replica pair
Es162en.getName () returns the name
when the database is being operated on db.getPrevError() returns the previous error object
db. getProfilingLevel ()? What grade
db. getReplicationInfo ()? What information
db.getSisterDB(name) get the db at the same server as this onew
Es176en.killOp () stops (kills) the current operation
in the current library Es179en.printCollectionStats () returns the current library's dataset status
db.printReplicationInfo()
db.printSlaveReplicationInfo()
Es186en.printShardingStatus () returns whether the current database is a Shared database
Es189en.removeUser (username) delete user
Es193en.repairDatabase () fixes 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
Es202en.shutdownServer () closes the current service program
Es205en.version () returns version information for the current program

Data set (table) operation syntax
Es211en.linlin.find ({id:10}) returns the linlin dataset ID=10 Es218en.linlin.find ({id:10}).count() returns the total number of data
for linlin dataset ID=10 Es226en.linlin.find ({id:10}).limit(2) returns linlin data set ID=10 data set
starting from the second data set Es234en.linlin.find ({id:10}).skip(8) returns linlin data set ID=10 data set
from 0 to 8 Es242en.linlin.find ({id:10}).limit(2).skip(8) returns linlin data set ID= 2 to 8 data set
Es251en.linlin.find ({id:10}).sort() returns the ordered data set
of linlin dataset ID=10 Es259en.linlin.findOne ([query]) returns a qualified piece of data
Es264en.linlin.getDB () returns the database name
to which this dataset belongs Es268en.linlin.getIndexes () returns the index information
for some data sets db.linlin.group({key:...,initial:...,reduce:...[,cond:...]})
db.linlin.mapReduce(mayFunction,reduceFunction, < optional params > )
Es280en.linlin.remove (query) delete a single piece of data
in the dataset Es285en.linlin.renameCollection (newName) rename some data sets
Es290en.linlin.save (obj) inserts a piece of data
into the data set Es295en.linlin.stats () returns the state of this dataset
Es299en.linlin.storageSize () returns the storage size of this dataset
Es303en.linlin.totalIndexSize () returns the index file size
for this dataset Es307en.linlin.totalSize () returns the total size of some data sets
Es311en.linlin.update (query,object[,upsert_bool]) updates a piece of data
in this dataset Es319en.linlin.validate () verify this dataset
Es323en.linlin.getShardVersion () returns the Shared version number
of the dataset
db.linlin.find({'name':'foobar'}) select * from linlin where name='foobar'
db.linlin.find() select * from linlin
db.linlin.find({'ID':10}).count() select count(*) from linlin where ID=10
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:20}}) 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':25}) insert into linlin ('name','age') values('foobar',25)
db.linlin.insert({'name':'foobar','age':25,'email':'cclove2@163.com'})

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

db.linlin.update({'name':'foobar'},{$set:{'age':36}}) 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'

official operation statement comparison table:

uplink: SQL action statement
Descending: 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 users WHERE name LIKE "%Joe%"
db.users.find({name:/Joe/})

SELECT * FROM users WHERE name LIKE "Joe%"
db.users.find({name:/^Joe/})

SELECT * FROM users WHERE age > 33 AND age < =40
db.users.find({'age':{$gt:33,$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 10 SKIP 20
db.users.find().limit(10).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 users where AGE > 30
db.users.find({age: {'$gt': 30}}).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 articles: