mongodb要如何去查询数据-创新互联
创新互联www.cdcxhl.cn八线动态BGP香港云服务器提供商,新人活动买多久送多久,划算不套路!
创新互联从2013年开始,是专业互联网技术服务公司,拥有项目网站制作、成都网站设计网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元潢川做网站,已为上家服务,为潢川各地企业和个人服务,联系电话:028-86922220mongodb要如何去查询数据?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
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要如何去查询数据问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联-成都网站建设公司行业资讯频道了解更多相关知识。
分享文章:mongodb要如何去查询数据-创新互联
当前地址:http://cdiso.cn/article/hchsc.html