您好,登錄后才能下訂單哦!
mongodb要如何去查詢數據?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
1、插入測試數據
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']})
添加數據的方式:普通數據的添加、多維數據的添加、數組數據的添加
2、MongoDB命令行查詢數據
MongoDB命令行查詢數據有以下幾種方式:
·籠統方式查詢
·條件限制查詢
·范圍查詢
·多個查詢條件
·多維字段的查詢
·數組條件的限制查詢
·字段的限制查詢
1)籠統方式查詢:
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 } ……
采用這種方式查詢,會查詢所有數據。
db.goods.findOne()
查詢所有數據中的第一條,并且將數據格式化輸出。
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字段的內容值是MongoDB本身算法獲得出來的,_id字段對應的值是“全球唯一”的,相當于MySQL表里的主鍵id,是唯一的。id可以自行設置,但是不推薦。
優點:方便MongoDB的數據做升級、遷移
3)范圍查詢
查詢goods表中價格大于10的數據:
db.goods.find({price:{'$gt':10}})
查詢goods表中價格大于等于10的數據:
db.goods.find({price:{'$gte':10}})
查詢goods表中價格小于10的數據:
db.goods.find({price:{'$lt':12}})
查詢goods表中價格小于等于10的數據:
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)數組條件的限制查詢
查詢“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字段除外,其它字段可以隨意設置0,1。可以設置全部字段都輸出,也設置為全部字段都不輸出。但是,字段中一個是1,另一個是0,這是不可以。
關于mongodb要如何去查詢數據問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。