麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 數(shù)據(jù)庫 > MongoDB > 正文

MongoDB 語法使用小結(jié)

2020-10-29 18:54:57
字體:
供稿:網(wǎng)友
他支持的數(shù)據(jù)結(jié)構(gòu)非常松散,是類似json的bjson格式,因此可以存儲比較復雜的數(shù)據(jù)類型。Mongo最大的特點是他支持的查詢語言非常強大,其語法有點類似于面向?qū)ο蟮牟樵冋Z言,幾乎可以實現(xiàn)類似關系數(shù)據(jù)庫單表查詢的絕大部分功能,而且還支持對數(shù)據(jù)建立索引。
它的特點是高性能、易部署、易使用,存儲數(shù)據(jù)非常方便。

1. MongoDB的獲取和安裝

(1)獲取地址 http://www.mongodb.org/downloads 根據(jù)自己需要選擇相應的版本,linux下可以使用wget 命令。
(2)解壓 mongodb-win32-i386-1.8.1
(3)創(chuàng)建數(shù)據(jù)存放文件夾,mongodb默認的數(shù)據(jù)目錄 /data/db
C:/> mkdir /data
C:/> mkdir /data/db
(4)運行 MongoDB
mongod.exe - 數(shù)據(jù)庫的服務器端,相當于mysql的 mysqld命令,啟動服務器端
mongo.exe - 數(shù)據(jù)庫的客戶端,相當于mysql的mysql命令,打開管理控制臺

啟動服務
mongod.exe --dbpath F:/DataBase/MongoDB/db/
--dbpath 數(shù)據(jù)文件存放路徑
--port 數(shù)據(jù)服務端口
C:/> cd /my_mongo_dir/bin
C:/my_mongo_dir/bin > mongod //啟動mongod 服務器,默認的數(shù)據(jù)庫路徑 /data/db,端口27071
啟動客戶端
mongo.exe cclove
cclove 所連接的數(shù)據(jù)庫名稱
C:/> cd /my_mongo_dir/bin
C:/my_mongo_dir/bin> mongo

2. 熟悉MongoDB的數(shù)據(jù)操作語句,類sql

數(shù)據(jù)庫操作語法
mongo --path
db.AddUser(username,password) 添加用戶
db.auth(usrename,password) 設置數(shù)據(jù)庫連接驗證
db.cloneDataBase(fromhost) 從目標服務器克隆一個數(shù)據(jù)庫
db.commandHelp(name) returns the help for the command
db.copyDatabase(fromdb,todb,fromhost) 復制數(shù)據(jù)庫fromdb---源數(shù)據(jù)庫名稱,todb---目標數(shù)據(jù)庫名稱,fromhost---源數(shù)據(jù)庫服務器地址
db.createCollection(name,{size:3333,capped:333,max:88888}) 創(chuàng)建一個數(shù)據(jù)集,相當于一個表
db.currentOp() 取消當前庫的當前操作
db.dropDataBase() 刪除當前數(shù)據(jù)庫
db.eval(func,args) run code server-side
db.getCollection(cname) 取得一個數(shù)據(jù)集合,同用法:db['cname'] or db.cname
db.getCollenctionNames() 取得所有數(shù)據(jù)集合的名稱列表
db.getLastError() 返回最后一個錯誤的提示消息
db.getLastErrorObj() 返回最后一個錯誤的對象
db.getMongo() 取得當前服務器的連接對象get the server connection object
db.getMondo().setSlaveOk() allow this connection to read from then nonmaster membr of a replica pair
db.getName() 返回當操作數(shù)據(jù)庫的名稱
db.getPrevError() 返回上一個錯誤對象
db.getProfilingLevel() ?什么等級
db.getReplicationInfo() ?什么信息
db.getSisterDB(name) get the db at the same server as this onew
db.killOp() 停止(殺死)在當前庫的當前操作
db.printCollectionStats() 返回當前庫的數(shù)據(jù)集狀態(tài)
db.printReplicationInfo()
db.printSlaveReplicationInfo()
db.printShardingStatus() 返回當前數(shù)據(jù)庫是否為共享數(shù)據(jù)庫
db.removeUser(username) 刪除用戶
db.repairDatabase() 修復當前數(shù)據(jù)庫
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() 關閉當前服務程序
db.version() 返回當前程序的版本信息

數(shù)據(jù)集(表)操作語法
db.linlin.find({id:10}) 返回linlin數(shù)據(jù)集ID=10的數(shù)據(jù)集
db.linlin.find({id:10}).count() 返回linlin數(shù)據(jù)集ID=10的數(shù)據(jù)總數(shù)
db.linlin.find({id:10}).limit(2) 返回linlin數(shù)據(jù)集ID=10的數(shù)據(jù)集從第二條開始的數(shù)據(jù)集
db.linlin.find({id:10}).skip(8) 返回linlin數(shù)據(jù)集ID=10的數(shù)據(jù)集從0到第八條的數(shù)據(jù)集
db.linlin.find({id:10}).limit(2).skip(8) 返回linlin數(shù)據(jù)集ID=1=的數(shù)據(jù)集從第二條到第八條的數(shù)據(jù)
db.linlin.find({id:10}).sort() 返回linlin數(shù)據(jù)集ID=10的排序數(shù)據(jù)集
db.linlin.findOne([query]) 返回符合條件的一條數(shù)據(jù)
db.linlin.getDB() 返回此數(shù)據(jù)集所屬的數(shù)據(jù)庫名稱
db.linlin.getIndexes() 返回些數(shù)據(jù)集的索引信息
db.linlin.group({key:...,initial:...,reduce:...[,cond:...]})
db.linlin.mapReduce(mayFunction,reduceFunction,<optional params>)
db.linlin.remove(query) 在數(shù)據(jù)集中刪除一條數(shù)據(jù)
db.linlin.renameCollection(newName) 重命名些數(shù)據(jù)集名稱
db.linlin.save(obj) 往數(shù)據(jù)集中插入一條數(shù)據(jù)
db.linlin.stats() 返回此數(shù)據(jù)集的狀態(tài)
db.linlin.storageSize() 返回此數(shù)據(jù)集的存儲大小
db.linlin.totalIndexSize() 返回此數(shù)據(jù)集的索引文件大小
db.linlin.totalSize() 返回些數(shù)據(jù)集的總大小
db.linlin.update(query,object[,upsert_bool]) 在此數(shù)據(jù)集中更新一條數(shù)據(jù)
db.linlin.validate() 驗證此數(shù)據(jù)集
db.linlin.getShardVersion() 返回數(shù)據(jù)集共享版本號

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
db.linlin.find().skip(10).limit(20) 從查詢結(jié)果的第十條開始讀20條數(shù)據(jù) 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':'[email protected]'})

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'

官方提供的操作語句對照表:

上行:SQL 操作語句
下行:Mongo 操作語句
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'});
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 免费观看国产精品视频 | 午夜九九九 | 激情小说图 | 色av综合在线 | 黄色大片免费网站 | 国产精品自拍av | xxxx hd videos| 狠狠干五月 | 欧美成人小视频 | 999久久久久久 | 福利在线国产 | 欧美成人鲁丝片在线观看 | 一色视频 | 99re66热这里只有精品8 | 亚洲一区二区 | 欧美激情999 | 欧美黄一区 | 欧美18一19sex性护士农村 | 国产成年人在线观看 | 国产羞羞视频在线免费观看 | 日韩一级免费毛片 | 日本成人一二三区 | a集毛片 | 黄色一级片免费在线观看 | 毛片一级网站 | 色中色激情影院 | 久久精品亚洲国产奇米99 | 99国产精品白浆在线观看免费 | 一级毛片真人免费播放视频 | 最新中文字幕在线 | 亚洲射情 | 黄色特级一级片 | 一级黄色免费观看 | 99seav| 黑人日比 | 欧美日韩免费在线观看视频 | www.99xxxx.com| 激情毛片 | hdhdhd79xxxxх| 国产亚洲精品久久久久久网站 | 国产pron |