創(chuàng)新互聯(lián)www.cdcxhl.cn八線動態(tài)BGP香港云服務(wù)器提供商,新人活動買多久送多久,劃算不套路!
創(chuàng)新互聯(lián)從2013年開始,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項目網(wǎng)站制作、成都網(wǎng)站設(shè)計網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元潢川做網(wǎng)站,已為上家服務(wù),為潢川各地企業(yè)和個人服務(wù),聯(lián)系電話:028-86922220mongodb要如何去查詢數(shù)據(jù)?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
1、插入測試數(shù)據(jù)
use flower
db.goods.insert({'goods_name':'Hyacinth',price:10,num:800}) db.goods.insert({goods_name:'Hyacinth',price:10,num:800}) db.goods.insert({goods_name:'violet',price:20,num:1000}) db.goods.insert({goods_name:'Plum blossom',price:20,num:1000}) db.goods.insert({goods_name:'Datura',price:40,num:700}) db.goods.insert({goods_name:'Rosemary',price:39,num:2000}) db.goods.insert({goods_name:'Rosemary',price:39,num:2000,area:{province:'beijing','city':'beijing'}}) db.goods.insert({goods_name:'Rosemary',price:39,num:2000,area:{province:'Shanghai','city':'Shanghai'}}) db.goods.insert({goods_name:'Chinese rose',price:67,num:3700,area:{province:'Henan','city':'Luoyang'}}) db.goods.insert({goods_name:'Chinese rose',price:32,num:900,area:{province:'Henan','city':'Luoyang'}}) db.goods.insert({goods_name:'Chinese rose',price:32,num:900,area:{province:'Henan','city':'Luoyang'},color: ['write','red']}) db.goods.insert({goods_name:'Chinese rose',price:32,num:900,area:{province:'Henan','city':'Luoyang'},color: ['red','green']}) db.goods.insert({goods_name:'Chinese rose',price:32,num:900,area:{province:'Henan','city':'Luoyang'},color: ['write','red','grees']})
添加數(shù)據(jù)的方式:普通數(shù)據(jù)的添加、多維數(shù)據(jù)的添加、數(shù)組數(shù)據(jù)的添加
2、MongoDB命令行查詢數(shù)據(jù)
MongoDB命令行查詢數(shù)據(jù)有以下幾種方式:
·籠統(tǒng)方式查詢
·條件限制查詢
·范圍查詢
·多個查詢條件
·多維字段的查詢
·數(shù)組條件的限制查詢
·字段的限制查詢
1)籠統(tǒng)方式查詢:
db.goods.find()
db.goods.find() { "_id" : ObjectId("5cb5bba28a0ad00bec55f14b"), "goods_name" : "Hyacinth", "price" : 10, "num" : 800 } { "_id" : ObjectId("5cb5bba28a0ad00bec55f14c"), "goods_name" : "Hyacinth", "price" : 10, "num" : 800 } { "_id" : ObjectId("5cb5bba28a0ad00bec55f14d"), "goods_name" : "violet", "price" : 20, "num" : 1000 } ……
采用這種方式查詢,會查詢所有數(shù)據(jù)。
db.goods.findOne()
查詢所有數(shù)據(jù)中的第一條,并且將數(shù)據(jù)格式化輸出。
db.goods.findOne() { "_id" : ObjectId("5cb5bba28a0ad00bec55f14b"), "goods_name" : "Hyacinth", "price" : 10, "num" : 800 }
2)條件限制查詢
db.goods.find({goods_name:'Hyacinth'})
{ "_id" : ObjectId("5cb5bba28a0ad00bec55f14b"), "goods_name" : "Hyacinth", "price" : 10, "num" : 800 } { "_id" : ObjectId("5cb5bba28a0ad00bec55f14c"), "goods_name" : "Hyacinth", "price" : 10, "num" : 800 }
相當于:SELECT *FROM goods WHERE goods_name='Hyacinth'
db.goods.findOne({goods_name:'Hyacinth'})
{ "_id" : ObjectId("5cb5bba28a0ad00bec55f14b"), "goods_name" : "Hyacinth", "price" : 10, "num" : 800 }
該_id字段的內(nèi)容值是MongoDB本身算法獲得出來的,_id字段對應的值是“全球唯一”的,相當于MySQL表里的主鍵id,是唯一的。id可以自行設(shè)置,但是不推薦。
優(yōu)點:方便MongoDB的數(shù)據(jù)做升級、遷移
3)范圍查詢
查詢goods表中價格大于10的數(shù)據(jù):
db.goods.find({price:{'$gt':10}})
查詢goods表中價格大于等于10的數(shù)據(jù):
db.goods.find({price:{'$gte':10}})
查詢goods表中價格小于10的數(shù)據(jù):
db.goods.find({price:{'$lt':12}})
查詢goods表中價格小于等于10的數(shù)據(jù):
db.goods.find({price:{'$lte':12}})
4)多個查詢條件
例如:要求price大于10,庫存大于800
db.goods.find({price:{'$gt':10},num:{'$gt':800}})
要求price大于10,庫存為900
db.goods.find({price:{'$gt':10},num:900})
5)多維字段的查詢
db.goods.find({'area.province':'Shanghai'})
db.goods.find({'area.city':'Shanghai'})
6)數(shù)組條件的限制查詢
查詢“color”中有“red”記錄,滿足一個元素即可:
db.goods.find({color:'red'})
查詢“color”中有“red”并且(AND)有“write”的記錄:
db.goods.find({color:{'$all':['red','write']}})
查詢“price”是10或者(OR)“num”是800
db.goods.find({$or:[{'price':10},{'num':800}]})
7)字段的限制查詢
db.goods.find({price:32},{goods_name:1})
{ "_id" : ObjectId("5cb5bba28a0ad00bec55f154"), "goods_name" : "Chinese rose" } { "_id" : ObjectId("5cb5bba28a0ad00bec55f155"), "goods_name" : "Chinese rose" } { "_id" : ObjectId("5cb5bba28a0ad00bec55f156"), "goods_name" : "Chinese rose" } { "_id" : ObjectId("5cb5bba48a0ad00bec55f157"), "goods_name" : "Chinese rose" }
db.goods.find({price:32},{goods_name:1,_id:0})
{ "goods_name" : "Chinese rose" } { "goods_name" : "Chinese rose" } { "goods_name" : "Chinese rose" } { "goods_name" : "Chinese rose" }
db.goods.find({price:{'$gt':30}},{goods_name:1,_id:0})
{ "goods_name" : "Datura" } { "goods_name" : "Rosemary" } { "goods_name" : "Rosemary" } { "goods_name" : "Rosemary" } { "goods_name" : "Chinese rose" } { "goods_name" : "Chinese rose" } { "goods_name" : "Chinese rose" } { "goods_name" : "Chinese rose" } { "goods_name" : "Chinese rose" }
db.goods.find({price:{'$gt':30}},{goods_name:1,price:1,_id:0})
{ "goods_name" : "Datura", "price" : 40 } { "goods_name" : "Rosemary", "price" : 39 } { "goods_name" : "Rosemary", "price" : 39 } { "goods_name" : "Rosemary", "price" : 39 } { "goods_name" : "Chinese rose", "price" : 67 } { "goods_name" : "Chinese rose", "price" : 32 } { "goods_name" : "Chinese rose", "price" : 32 } { "goods_name" : "Chinese rose", "price" : 32 } { "goods_name" : "Chinese rose", "price" : 32 }
說明:_id字段除外,其它字段可以隨意設(shè)置0,1??梢栽O(shè)置全部字段都輸出,也設(shè)置為全部字段都不輸出。但是,字段中一個是1,另一個是0,這是不可以。
關(guān)于mongodb要如何去查詢數(shù)據(jù)問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司行業(yè)資訊頻道了解更多相關(guān)知識。
當前標題:mongodb要如何去查詢數(shù)據(jù)-創(chuàng)新互聯(lián)
文章源于:http://aaarwkj.com/article32/hchsc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營銷、小程序開發(fā)、定制開發(fā)、網(wǎng)站排名、建站公司、網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容