您好,登錄后才能下訂單哦!
使用library數(shù)據(jù)庫
use libraray
查看可用數(shù)據(jù)庫和集合
show dbs show collections
插入數(shù)據(jù)insert操作
兩種定義文檔的格式:
document = ( { "Type" : "Book", "Title" : "Definitive Guide to MongoDB 2nd ed.,The", "ISBN" : "978-1-4302-5821-6", "Publisher" : "Apress", "Author": ["Hows, David","Plugge, Eelco", "Membrey, Peter", "Hawkins, Tim" ] } ) document = ( { "Type" : "Book", "Title" : "Definitive Guide to MongoDB 2nd ed.,The", "ISBN" : "978-1-4302-5821-6", "Publisher" : "Apress", "Author": ["Hows, David","Plugge, Eelco", "Membrey, Peter", "Hawkins, Tim" ] } )
直接使用insert插入:
db.media.insert(document)
也可以不定義文檔直接插入:
db.media.insert({ "Type" : "CD", "Artist" : "Nirvana", "Title" : "Nevermind" })
或:
db.media.insert( { "Type" : "CD", "Artist" : "Nirvana", "Title" : "Nevermind", "Tracklist" : [ { "Track" : "1", "Title" : "Smells Like Teen Spirit", "Length" : "5:02" }, { "Track" : "2", "Title" : "In Bloom", "Length" : "4:15" } ] })
查詢數(shù)據(jù)find操作
查詢所有數(shù)據(jù):
db.media.find()
查詢特定的文檔:
db.media.find({Artist:"Nirvana"})
查看特定的列
db.media.find({Artist:"Nirvana"},{Title:1})
只要在find中使用第二個(gè)參數(shù){Title:1}返回標(biāo)題字段信息即可。將1改為0為不顯示該標(biāo)題
具體查詢(有點(diǎn)像SQL的where條件):
db.media.find({"Tracklist.Title":"In Bloom"})
查詢Tracklist下Title為“In Bloom"的數(shù)據(jù)
db.media.find({"Author":"Membrey, Peter"})
查詢數(shù)組Author中含有"Membrey, Peter"的文檔
使用sort、limit、skip函數(shù)
排序:
db.media.find().sort({Title:1})
升序,只要將Title:1改成-1就為倒序
限制輸出n個(gè)文檔:
db.media.find().limit(2)
不限顯示第一個(gè)文檔:
db.media.find().skip(1)
三個(gè)函數(shù)結(jié)合一起使用:
安Titile倒序,顯示10個(gè)文檔,跳過頭一個(gè)
db.media.find().sort({Title:-1}).limit(10).skip(1)
固定順序集合:
創(chuàng)建固定順序集合:
db.createCollection("audit",{capped:true,size:204800})
這種集合能保證插入的文檔按照插入順序排序,不會(huì)在以后更新中改變順序。
聚集函數(shù)(類似SQL的Group by):
count函數(shù):
db.media.count()
返回當(dāng)前media集合有多少個(gè)文檔
指定符合條件的文檔數(shù):
db.media.find({Publisher:"Apress"}).count()
注意count會(huì)忽略skip與limit,例如:
db.media.find().skip(1).count() 等價(jià)db.media.find().count()
不想忽略skip跟limit需要在count里面添加true參數(shù),例如:
db.media.find().skip(1).count(true)
去重復(fù)distinct函數(shù):
增加一個(gè)文檔:
document = ( { "Type" : "Book", "Title" : "Definitive Guide to MongoDB 2nd ed.,The", "ISBN" : "978-1-4302-5821-6", "Publisher" : "Apress", "Author": ["Hows, David","Plugge, Eelco", "Membrey, Peter", "Hawkins, Tim" ] } ) db.media.insert(document) db.media.distinct("Title")
這樣相同內(nèi)容的Title就會(huì)合并成一條記錄
group函數(shù):
db.media.group({key:{Title:true}, initial:{Total:0}, reduce:function(items,prev) { prev.Total+=200; } } )
條件查詢:
添加一些數(shù)據(jù):
dvd=({ "Type" : "DVD", "Title" : "Matrix,The", "Released":1999, "Cast":["Keanu Reeves","Carrie-Anne Moss","Laurence Fishburne","Hugo Weaving","Gloria Foster","Joe Pantoliano"]}) dvd=({"Type":"DVD",Title:"Blade Runner",Released:1992}) db.media.insert(dvd) dvd=({"Type":"DVD",Title:"Toy Story 3",Released:2010}) db.media.insert(dvd)
大于操作:
db.media.find({Released:{$gt:2000}})
小于:
db.media.find({Released:{$lt:2000}})
gte與lte是大于等于,小于等于
指定范圍在大于等于1990到小于等于2010之間:
db.media.find({Released:{$gte:1990,$lte:2010}})
否定查詢$ne:
db.media.find({Tyep:"Book",Author:{$ne:"Plugge,Eelco"}})
數(shù)組的操作:
$in 其中一個(gè)匹配即可
db.media.find({Released:{$in:[1999,2008,2009]}})
$nin全都不匹配
db.media.find({Released:{$nin:[1999,2008,2009]}})
$all匹配所有:
db.media.find({Released:{$all:[1999,2008,2009]}})
上述語句不會(huì)返回任何數(shù)據(jù)
$or 多條件搜索:
db.media.find({$or:[{"Title":"Toy Story 3"},{"ISBN":"978-1-4302-5821-6"}]})
添加參數(shù)限制類型:
db.media.find({ "Type" : "DVD",$or:[{"Title":"Toy Story 3"},{"ISBN":"978-1-4302-5821-6"}]})
$slice操作數(shù)組范圍:
顯示前3項(xiàng):
db.media.find({"Title":"Matrix,The"},{"Cast":{$slice:3}})
后三項(xiàng):
db.media.find({"Title":"Matrix,The"},{"Cast":{$slice:-3}})
忽略前2項(xiàng),從開始的3個(gè)數(shù)據(jù)項(xiàng):
db.media.find({"Title":"Matrix,The"},{"Cast":{$slice:[2,3]}})
從倒數(shù)第5個(gè)開始輸出4個(gè)數(shù)據(jù):
db.media.find({"Title":"Matrix,The"},{"Cast":{$slice:[-5,4]}})
奇偶數(shù)操作:
偶數(shù)的年份:
db.media.find({Released:{$mod:[2,0]}})
奇數(shù):
db.media.find({Released:{$mod:[2,1]}})
查詢數(shù)組元素?cái)?shù)量符合特定值:
db.media.find({Tracklist:{$size:2}})
Tracklist數(shù)組只有兩個(gè)元素的文檔
返回特定對(duì)象存在:
db.media.find({Author:{$exists:true}})
匹配完整的數(shù)組
添加一個(gè)文檔:
nirvana = ( { "Type" : "CD", "Artist" : "Nirvana", "Title" : "Nirvana","Tracklist" : [ { "Track" : "1", "Title" : "You Know You're Right", "Length": "3:38"}, {"Track" : "5", "Title" : "Smells Like Teen Spirit", "Length" :"5:02" } ] } )
查找Tracklist.Title為"Smells Like Teen Spirit"并且為Track 1的數(shù)據(jù)將會(huì)有多行重復(fù)
利用$elemMatch可以匹配數(shù)組中的完整文檔
db.media.find({"Tracklist":{"$elemMatch":{Title:"Smells Like Teen Spirit","Track":"1"}}})
可以使用$not否定該條件,例如:
db.media.find({"Tracklist":{$not:{"$elemMatch":{Title:"Smells Like Teen Spirit","Track":"1" }}}})
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。