學習一個數(shù)據(jù)庫,首先要會的是如何連接數(shù)據(jù)庫,如何進行基本的增刪改查,其它高級功能慢慢學習即可,
mongodb基本增刪改查
。下面主要講解mogodb基本的增刪改查
1、創(chuàng)建數(shù)據(jù)庫:use mkyongdb (有則跳轉(zhuǎn)到這個數(shù)據(jù)庫,沒有的話創(chuàng)建并跳轉(zhuǎn))
顯示所有數(shù)據(jù)庫:show dbs
2、創(chuàng)建集合(相當于關系數(shù)據(jù)庫的表):
集合操作:
(1)插入數(shù)據(jù)
db.users.insert({'name':'xumingxiang','sex':'man'}) 創(chuàng)建一個users的集合
顯示當前數(shù)據(jù)庫下的所有集合:show collections
顯示users集合下的所有數(shù)據(jù)文檔:db.users.find()
顯示集合的記錄數(shù):db.users.count()
(2)更新數(shù)據(jù)
db.users.update({'name':'xiangshu'},{'$set':{'sex':'women'}},upsert=true,multi=false)
第一:查詢的條件
第二:更新的字段
第三:如果不存在則插入
第四:是否允許修改多條記錄
(3)刪除數(shù)據(jù)
db. users.remove({'name':'xumingxiang'})
(4)刪除所有記錄
db.users.remove()
刪除collection
db.users.drop() //如果刪除成功會返回“true”,否則返回“false”
刪除當前數(shù)據(jù)庫
db.dropDatabase()
NOSQL和SQL數(shù)據(jù)庫比較(PS:參照網(wǎng)上的)
MongoDB語法 MySql語法
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'