最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

MongoDB 語法使用小結(jié)

 更新時(shí)間:2011年10月27日 22:26:01   作者:  
MongoDB是一個(gè)介于關(guān)系數(shù)據(jù)庫和非關(guān)系數(shù)據(jù)庫之間的產(chǎn)品,是非關(guān)系數(shù)據(jù)庫當(dāng)中功能最豐富,最像關(guān)系數(shù)據(jù)庫的
他支持的數(shù)據(jù)結(jié)構(gòu)非常松散,是類似json的bjson格式,因此可以存儲(chǔ)比較復(fù)雜的數(shù)據(jù)類型。Mongo最大的特點(diǎn)是他支持的查詢語言非常強(qiáng)大,其語法有點(diǎn)類似于面向?qū)ο蟮牟樵冋Z言,幾乎可以實(shí)現(xiàn)類似關(guān)系數(shù)據(jù)庫單表查詢的絕大部分功能,而且還支持對數(shù)據(jù)建立索引。
它的特點(diǎn)是高性能、易部署、易使用,存儲(chǔ)數(shù)據(jù)非常方便。

1. MongoDB的獲取和安裝

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

啟動(dòng)服務(wù)
mongod.exe --dbpath F:/DataBase/MongoDB/db/
--dbpath 數(shù)據(jù)文件存放路徑
--port 數(shù)據(jù)服務(wù)端口
C:/> cd /my_mongo_dir/bin
C:/my_mongo_dir/bin > mongod //啟動(dòng)mongod 服務(wù)器,默認(rèn)的數(shù)據(jù)庫路徑 /data/db,端口27071
啟動(dòng)客戶端
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è)置數(shù)據(jù)庫連接驗(yàn)證
db.cloneDataBase(fromhost) 從目標(biāo)服務(wù)器克隆一個(gè)數(shù)據(jù)庫
db.commandHelp(name) returns the help for the command
db.copyDatabase(fromdb,todb,fromhost) 復(fù)制數(shù)據(jù)庫fromdb---源數(shù)據(jù)庫名稱,todb---目標(biāo)數(shù)據(jù)庫名稱,fromhost---源數(shù)據(jù)庫服務(wù)器地址
db.createCollection(name,{size:3333,capped:333,max:88888}) 創(chuàng)建一個(gè)數(shù)據(jù)集,相當(dāng)于一個(gè)表
db.currentOp() 取消當(dāng)前庫的當(dāng)前操作
db.dropDataBase() 刪除當(dāng)前數(shù)據(jù)庫
db.eval(func,args) run code server-side
db.getCollection(cname) 取得一個(gè)數(shù)據(jù)集合,同用法:db['cname'] or db.cname
db.getCollenctionNames() 取得所有數(shù)據(jù)集合的名稱列表
db.getLastError() 返回最后一個(gè)錯(cuò)誤的提示消息
db.getLastErrorObj() 返回最后一個(gè)錯(cuò)誤的對象
db.getMongo() 取得當(dāng)前服務(wù)器的連接對象get the server connection object
db.getMondo().setSlaveOk() allow this connection to read from then nonmaster membr of a replica pair
db.getName() 返回當(dāng)操作數(shù)據(jù)庫的名稱
db.getPrevError() 返回上一個(gè)錯(cuò)誤對象
db.getProfilingLevel() ?什么等級
db.getReplicationInfo() ?什么信息
db.getSisterDB(name) get the db at the same server as this onew
db.killOp() 停止(殺死)在當(dāng)前庫的當(dāng)前操作
db.printCollectionStats() 返回當(dāng)前庫的數(shù)據(jù)集狀態(tài)
db.printReplicationInfo()
db.printSlaveReplicationInfo()
db.printShardingStatus() 返回當(dāng)前數(shù)據(jù)庫是否為共享數(shù)據(jù)庫
db.removeUser(username) 刪除用戶
db.repairDatabase() 修復(fù)當(dāng)前數(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() 關(guān)閉當(dāng)前服務(wù)程序
db.version() 返回當(dāng)前程序的版本信息

數(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ù)集的存儲(chǔ)大小
db.linlin.totalIndexSize() 返回此數(shù)據(jù)集的索引文件大小
db.linlin.totalSize() 返回些數(shù)據(jù)集的總大小
db.linlin.update(query,object[,upsert_bool]) 在此數(shù)據(jù)集中更新一條數(shù)據(jù)
db.linlin.validate() 驗(yàn)證此數(shù)據(jù)集
db.linlin.getShardVersion() 返回?cái)?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':'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'

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

上行: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'});

相關(guān)文章

  • MongoDB的安裝方法圖文教程

    MongoDB的安裝方法圖文教程

    MongoDB 是由C++語言編寫的,是一個(gè)基于分布式文件存儲(chǔ)的開源數(shù)據(jù)庫系統(tǒng)。本文給大家介紹了mongodb的安裝教程,非常不錯(cuò),具有參考借鑒價(jià)值,感興趣的朋友一起看看吧
    2016-11-11
  • MongoDB實(shí)現(xiàn)增刪改查

    MongoDB實(shí)現(xiàn)增刪改查

    本文詳細(xì)講解了MongoDB實(shí)現(xiàn)增刪改查的方法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-04-04
  • mongodb中隨機(jī)獲取1條記錄的實(shí)現(xiàn)方法

    mongodb中隨機(jī)獲取1條記錄的實(shí)現(xiàn)方法

    這篇文章運(yùn)用實(shí)例給大家演示了如何在mongodb中隨機(jī)獲取1條記錄,文中介紹的很詳細(xì),有需要的朋友們可以參考借鑒。下面來一起看看吧。
    2016-09-09
  • MongoDB數(shù)據(jù)庫部署環(huán)境準(zhǔn)備及使用介紹

    MongoDB數(shù)據(jù)庫部署環(huán)境準(zhǔn)備及使用介紹

    這篇文章主要為大家介紹了MongoDB數(shù)據(jù)庫部署環(huán)境準(zhǔn)備以及基本的使用介紹,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-03-03
  • 基于Morphia實(shí)現(xiàn)MongoDB按小時(shí)、按天聚合操作方法

    基于Morphia實(shí)現(xiàn)MongoDB按小時(shí)、按天聚合操作方法

    這篇文章主要介紹了基于Morphia實(shí)現(xiàn)MongoDB按小時(shí)、按天聚合操作 ,實(shí)現(xiàn)思路是啟動(dòng)定時(shí)任務(wù),對各用戶的設(shè)備狀態(tài)數(shù)據(jù)分別按照小時(shí)以及天進(jìn)行聚合,并存儲(chǔ)進(jìn)數(shù)據(jù)庫中供用戶后續(xù)查詢.需要的朋友可以參考下
    2019-07-07
  • MongoDB常用命令小結(jié)

    MongoDB常用命令小結(jié)

    這篇文章主要介紹了MongoDB的一些常用命令,學(xué)習(xí)與使用MongoDB數(shù)據(jù)庫的朋友可以參考下
    2013-08-08
  • MongoDB數(shù)據(jù)庫性能監(jiān)控詳解

    MongoDB數(shù)據(jù)庫性能監(jiān)控詳解

    MongoDB作為圖片和文檔的存儲(chǔ)數(shù)據(jù)庫,為啥不直接存MySQL里,還要搭個(gè)MongoDB集群,麻不麻煩?這篇文章就帶你介紹MongoDB數(shù)據(jù)庫性能監(jiān)控,感興趣的同學(xué)可以參考閱讀
    2023-03-03
  • MongoDB4.2.5安裝方法操作步驟

    MongoDB4.2.5安裝方法操作步驟

    這篇文章主要介紹了MongoDB4.2.5安裝方法操作步驟,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-12-12
  • Mongodb?刪除文檔Delete與Remove的區(qū)別解析

    Mongodb?刪除文檔Delete與Remove的區(qū)別解析

    這篇文章主要介紹了Mongodb?刪除文檔Delete與Remove的區(qū)別,要從集合中刪除所有文檔,請將空過濾器文檔傳遞{}給該?db.collection.deleteMany()方法,本文通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-08-08
  • 給MongoDB添加用戶權(quán)限方法分享

    給MongoDB添加用戶權(quán)限方法分享

    這篇文章主要介紹了給MongoDB添加用戶權(quán)限方法分享,十分的細(xì)致全面,推薦給大家,有需要的小伙伴可以參考下。
    2015-03-03

最新評論

清丰县| 桃园市| 张家港市| 隆林| 阳谷县| 古蔺县| 淮南市| 霍邱县| 阜平县| 丘北县| 永登县| 贡觉县| 石柱| 固镇县| 黑龙江省| 滦南县| 木兰县| 达拉特旗| 湄潭县| 开远市| 津南区| 新密市| 河源市| 博野县| 玉屏| 华坪县| 岳西县| 同江市| 通道| 兰州市| 云南省| 镇原县| 文登市| 黄陵县| 普格县| 明水县| 建水县| 清水河县| 同仁县| 洞口县| 思南县|