MongoDB数据查询 --MongoDB

时间:2023-12-21 09:40:20
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,这是不可以。