MongoDB6.0 Windows10实验操作命令可复制


1 实验环境

Windows10
MongoDB6.0(Compass1.5)

1.1 基础操作

  1. 实验:MongoDB的客户端与服务端连接后,查看数据库列表:
  2. 实验:切换到sampleDB数据库,并查看该数据库中有哪些集合(collection)
show dbs
use sampleDB

在这里插入图片描述

  1. 示例:查看trips集合的文档(document)字段(field)组成:
db.trips.findOne()

在这里插入图片描述

实验:查看zips集合的文档字段组成

db.zips.findOne()

在这里插入图片描述

  1. 示例:查询trips集合中usertype字段等于"Subscriber"的所有文档,且只返回usertype字段
db.trips.find({usertype:"Subscriber"},{usertypr:1,_id})

在这里插入图片描述

实验:查询zips集合中,city等于 "ZEELAND"的所有文档,且只返回city和_id字段

db.zips.find({city:"ZEELAND"},{city:1})

在这里插入图片描述

  1. 示例:查询trips集合中usertype字段等于"Subscriber",且birth year大于1990的所有文档,且只返回usertype和birth year字段
db.find({usertype:"Subscriber","birth year":{$gt:1990}},{usertype:1,"birth year":1,_id:0})

在这里插入图片描述

实验:查询zips集合中,city等于 “ZEELAND”,且pop等于497的所有文档,且只返回city和pop字段

db.zips.find({city:"ZEELAND",pop:497},{city:1,pop:1,_id:0})

在这里插入图片描述

  1. 示例:查询trips集合中tripduration字段值小于100的所有文档,且只返回tripduration字段
db.trips.find({tripduration:{$lt:100}},{tripduration:1,_id:0})

在这里插入图片描述

实验:查询zips集合中,pop字段值大于6000的所有文档,且只返回pop字段

db.zips.find({pop:{$gt:6000}},{pop:1,_id:0})

在这里插入图片描述

1.2 逻辑运算符

$or - 或者, $and - 既…又…, $nor - 既不…也不…, $not - 不

  1. $or示例:查询trips集合中所有tripduration字段大于100000,或者小于70的文档,且只返回tripduration和_id字段。
db.trips.find({$or:[{tripduration:{$gt:100000}},
                    {tripduration:{$lt:70}}
                    ]},{tripduration:1})

在这里插入图片描述

实验:查询inspections集合中所有 result字段等于 “Unable to Locate” 或者certificate_number字段等于9278806的文档 ,且只返回result和certificate_number字段(注意区分大小写)

db.inspections.find({$or:[{result:"Unable to Locate"},
                          {certificate_number:9278806}
                          ]},{result:1,certificate_number:1,_id:0})

在这里插入图片描述

  1. $and示例:查询trips集合中所有tripduration字段大于2000且birth year字段等于1990的文档,且只返回tripduration和birth year字段
 db.trips.find({$and:[{tripduration:{$gt:2000}},
                      {"birth year";1990}
                       ]},{tripduration:1,birth year:1,_id:0})

在这里插入图片描述

实验(1):查询trips集合中所有tripduration字段大于300,且小于400的文档,并只返回tripduration字段

db.trips.find({$and:[{tripduration:{$gt:300}},
                     {tripduration:{$lt:400}}
                    ]},{tripduration:1,_id:0})

在这里插入图片描述

实验(2):对于实验(1),下面这种写法,结果是不是也一样?结果一致。

db.trips.find({tripduration:{$gt:300,$lt:400}},{tripduration:1,_id:0})

在这里插入图片描述

  1. $nor示例:查询trips集合中所有tripduration字段既不小于100000,且birth year字段不为空字符串的文档,且只返回tripduration和birth year字段
 db.trips.find({$nor:[{tripduration:{$lt:100000}},
                      {"birth year";""}
                       ]},{tripduration:1,birth year:1,_id:0})

在这里插入图片描述

实验(1):查询companies集合中所有满足 number_of_employees字段既不大于200,也不小于100的文档,且只返回number_of_employees字段。

db.companies.find({$nor:[{number_of_employees:{$gt:200}},
                         {number_of_employees:{$lt:100}}
                          ]},{number_of_employees:1,_id:0})

在这里插入图片描述

实验(2): 实验(1)的可以这样写吗?结果相同吗?不相同。

db.companies.find({$and:[{number_of_employees:{$gte:100}},
                         {number_of_employees:{$lte:200}}
                           ]},{number_of_employees:1,_id:0})

在这里插入图片描述

实验(3): 如果上面两个实验结果不一样,请做完后面的$type实验后,再回来修改实验(1)的代码,让其与实验(2)的结果相同

$type:10等价于$eq:null
db.companies.find({$nor:[
                          {number_of_employees:{$gt:200}},
                          {number_of_employees:{$lt:100}},
                          {number_of_employees:{$type:10}}
                           ]},{number_of_employees:1,_id:0})

在这里插入图片描述

  1. $not示例:查询trips集合中所有birth year字段不等于1990的所有文档
db.trips.find({"birth year":{$not:{$eq:1990}}},{"birth year":1})

在这里插入图片描述

实验:上面的示例可以这样写吗?结果相同吗?相同。

db.trips.find({"birth year":{$ne:1990}},{"birth year":1})

在这里插入图片描述

1.3 $expr:表达式操作符

$expr:表达式操作符。可以用于同一文档中,字段值之间的比较

  1. 示例:查看routes集合文档的字段构成
db.routes.findOne()

在这里插入图片描述

实验:查看companies集合文档的字段构成:

db.companies.findOne()

在这里插入图片描述

  1. 示例:查询routes集合中所有src_airport和dst_airport字段值相等的文档,且只返回src_airport和dst_airport字段
db.routes.find({$expr:{$eq:["$src_airport","$dst_airport"]
                         }},{src_airport:1,dst_airport:1,_id:0})

在这里插入图片描述

实验(1):查询companies集合中所有name和permalink字段值相等的文档,且只返回name和permalink字段

db.companies.find({$expr:{$eq:["$name","$permalink"]}
                    },{name:1,permalink:1,_id:0})

在这里插入图片描述

实验(2):查询companies集合中所有founded_year小于deadpooled_year字段值的文档,且只返回founded_year和deadpooled_year字段

db.companies.find({$expr:{$gt:["founded_year","deadpooled_year"]
                           }},{founded_year:1,deadpooled_year:1,_id:0})

在这里插入图片描述

  1. 示例:查询routes集合中所有stops字段大于0的文档,并且只返回stops和_id字段(两种方法)
db.routes.find({stops:{$gt:0}},{stops:1})
db.routes.find({$expr:{$gt:["$stops,0"]}},{stops:1})

在这里插入图片描述
在这里插入图片描述
实验:查询companies集合中所有number_of_employees大于100000的文档,且只返回number_of_employees字段(两种方法)

db.companies.find({number_of_employees:{$gt:100000}},{number_of_employees:1,_id:0})
db.companies.find({$expr:{$gt:["$number_of_employees",100000]}},{number_of_employees:1,_id:0})

在这里插入图片描述

1.4 元素操作符

$exists - 返回(不)含有指定字段的文档 $type - 返回指定BSON字段类型的文档

  1. $exists示例(1):查询companies集合中所有含有ipo字段的文档
 db.companies.find({ipo:{$exists:true}})

在这里插入图片描述

示例(2):查询companies集合中所有不含有ipo字段的文档

 db.companies.find({ipo:{$exists:false}})

在这里插入图片描述

实验:查询companies集合中不含有ipo字段,并且number_of_employees字段小于50的文档

db.companies.find({$and:[{ipo:{$exists:false}},{number_of_employees:{$lt:50}}]},{number_of_employees:1})

在这里插入图片描述

  1. $type示例:查询companies集合中所有homepage_url字段为String类型的文档
db.companies.find({"homepage_url":{$type:2}},{homepage_url:1})

在这里插入图片描述

{ $type: 2 }中的2就表示字符串类型。
请访问:https://www.mongodb.com/docs/manual/reference/bson-types/查看各种类型对应的数值代号。

实验(1):查询companies集合中所有homepage_url字段不为null类型的文档(提示:$not)

db.companies.find({"homepage_url":{$not:{$type:10}}},{homepage_url:1})

在这里插入图片描述

实验(2):查询companies集合中所有number_of_employees字段值不等于100,且不为null类型的文档,且只返回number_of_employees字段

db.companies.find({$and:[
                          {number_of_employees:{$not:{$eq:100}}},
                          {number_of_employees:{$not:{$type:10}}}
                           ]},{number_of_employees:1,_id:0})

在这里插入图片描述

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值