MongoDB数据库中的文档查询操作

目录

  • 0 环境
  • 1 要求
  • 2 实例
    • 2.1 准备工作
      • 2.1.1 新建集合及插入数据
      • 2.1.2 查询集合内的数据
    • 2.2 解决问题
      • 2.2.1 统计集合中共有多少个文档数据,并定义游标,打印出所有数据
      • 2.2.2 创建以gnumber正序排列的单字段索引
      • 2.2.3 创建以gnumber逆序(1,升序)排列,price正序(-1,降序)排列的复合索引
      • 2.2.4 查询价格大于5的商品数据
      • 2.2.5 查询quantity为10且价格大于等于5的商品数据
      • 2.2.6 查询quantity为10或价格小于等于10的商品数据
      • 2.2.7 查询pnumber为“p003”且quantity为10或价格大于等于5的商品数据
      • 2.2.8 查看在查询中所用到的索引情况


0 环境

OS:Windows10

MongoDB:MongoDB-3.4.4
网盘链接:https://pan.baidu.com/s/1tPUBNjlwqSdhCS89LpS6pQ
提取码:cwhk

可视化界面:mongodb-compass-1.28.4-win32-x64
网盘链接:https://pan.baidu.com/s/1GZA1Y66GnhtitF2OLzFkig
提取码:jz89

(以上软件均可从MongoDB官网免费下载使用)


1 要求

  1. 使用 find() 方法进行文档基本查询
  2. 文档查询条件的使用
  3. 针对特定类型的文档进行查询
  4. 索引操作

2 实例

每个文档对应订单中某个商品相关信息,包括:

  • gnumber:商品编号
  • quantity:商品数量
  • price:商品单价

插入并查询如下商品信息:
MongoDB数据库中的文档查询操作_第1张图片


2.1 准备工作

2.1.1 新建集合及插入数据

db.items_wjw.insert({gnumber:"P003",quantity:2,price:5})
db.items_wjw.insert({gnumber:"P002",quantity:2,price:8})
db.items_wjw.insert({gnumber:"P002",quantity:1,price:4})
db.items_wjw.insert({gnumber:"P001",quantity:10,price:4})
db.items_wjw.insert({gnumber:"P003",quantity:4,price:10})
db.items_wjw.insert({gnumber:"P001",quantity:10,price:20})
db.items_wjw.insert({gnumber:"P003",quantity:10,price:20})
db.items_wjw.insert({gnumber:"P002",quantity:5,price:10})

MongoDB数据库中的文档查询操作_第2张图片

2.1.2 查询集合内的数据

db.items_wjw.find().pretty()

MongoDB数据库中的文档查询操作_第3张图片


2.2 解决问题

2.2.1 统计集合中共有多少个文档数据,并定义游标,打印出所有数据

var cursor = db.items_wjw.find()	//定义游标
print(cursor)		//打印游标变量
print(cursor.size())	//输出集合中文档的个数

在这里插入图片描述

while(cursor.hasNext()){
printjson(cursor.next())	//使用printjson输出结果数据集
}

MongoDB数据库中的文档查询操作_第4张图片

2.2.2 创建以gnumber正序排列的单字段索引

注:db.collection.createIndex(keys, options) //语法中 Key 值为你要创建的索引字段,1 为指定按升序(逆序)创建索引,如果你想按降序(正序)来创建索引指定为 -1 即可。

db.items_wjw.createIndex({gnumber:-1})

在这里插入图片描述

查看集合索引

db.items_wjw.getIndexes()

MongoDB数据库中的文档查询操作_第5张图片

2.2.3 创建以gnumber逆序(1,升序)排列,price正序(-1,降序)排列的复合索引

db.items_wjw.createIndex({gnumber:1,price:-1})

在这里插入图片描述

查看集合索引

db.items_wjw.getIndexes()

MongoDB数据库中的文档查询操作_第6张图片

2.2.4 查询价格大于5的商品数据

db.items_wjw.find({price:{$gt:5}}).pretty()

MongoDB数据库中的文档查询操作_第7张图片

2.2.5 查询quantity为10且价格大于等于5的商品数据

db.items_wjw.find({quantity:5,price:{$gte:5}}).pretty()

在这里插入图片描述

2.2.6 查询quantity为10或价格小于等于10的商品数据

db.items_wjw.find({$or:[{quantity:10},{price:{$lte:10}}]}).pretty()

MongoDB数据库中的文档查询操作_第8张图片

2.2.7 查询pnumber为“p003”且quantity为10或价格大于等于5的商品数据

db.items_wjw.find({$or:[{pnumber:"P003",quantity:10},{price:{$gte:5}}]}).pretty()

MongoDB数据库中的文档查询操作_第9张图片

2.2.8 查看在查询中所用到的索引情况

注:在查询语句后加上explain()方法即可查看在此查询中所用到的索引情况。

db.items_wjw.find({$or:[{pnumber:"P003",quantity:10},{price:{$gte:5}}]}).explain()

MongoDB数据库中的文档查询操作_第10张图片


总结:本文介绍了MongoDB数据库中的文档查询操作!

  1. 使用 find() 方法进行文档基本查询;
  2. 文档查询条件的使用;
  3. 针对特定类型的文档进行查询;
  4. 索引操作。

之前写的MongoDB数据库的有关博客:

  1. 安装MongodB数据库及简单操作
  2. MongoDB数据库中对数据库、集合和文档的操作

后续会继续更新有关MongoDB数据库的内容!
(注:第22次发文,如有错误和疑问,欢迎在评论区指出!)
——2021.11.29

你可能感兴趣的:(数据库,mongodb,数据库,nosql)