MongoDB的使用

发布时间:2020-08-05 08:47:14 作者:Winter
来源:ITPUB博客 阅读:158

今天来学习一个新的数据库,叫做MongoDB数据库,我们先来了解一下MongoDB数据库的概念,再一起学习如何使用MongoDB数据库吧~

MongoDB的概念

MongoDB的使用

MongoDB的命令介绍

db.help()                    查看库级别的命令
db.mycoll.help()             查看collection级别的命令
sh.help()                    查看发片的命令
rs.help()                    查看副本集的命令
help admin                   查看管理相关的命令
help connect                 查看连接到数据库的命令
help keys                    keys的相关命令
help misc                    misc things to know
help mr                      查看mapreduce相关的命令
show dbs                     查看当前的数据库
show collections             查看数据库中所有的collections
show users                   当前的数据库中有哪些用户
show profile                 显示profile信息,显示性能评估工具
show logs                    显示日志名信息
show log [name]              显示指定查看对应日志的信息
use <db_name>                进入某库,设定某库为当前库
db.foo.find()                列出当前collection中所有的document
db.foo.find( { a : 1 } )     列出当前collection中a = 1的document
it                           result of the last line evaluated; use to further iterate
DBQuery.shellBatchSize = x   设置显示的item的行数
exit                         退出Mongo shell

CRUD操作

Collection的简单查询过滤操作


db.students.remove({"name": "Angle"})
# 删除表
db.students.drop()
# 删除当前数据库
db.dropDatabase()

find()的高级用法

比较操作:


db.students.find({age: {$gt: 10}})  age大于10


db.students.find({age: {$gte: 20}})  age大于等于20


db.students.find({age: {$lt: 30}})  age小于30


db.students.find({age: {$lte: 40}})  age小于等于40


db.students.find({age: {$in: [10, 20]}})  age在[10, 20]的document


db.students.find({age: {$nin: [30, 40]}})  age不在[30, 40]的document

逻辑运算:


db.students.find({$or: [{name: {$eq: "robby"}}, {age: {$nin: [40,50]}}]})

元素查询:

如查询存在name字段的document
db.students.find({name: {$exists: true}})

update()的高级用法

将name为Tom的这个document的age字段的值改为20
db.students.update({name: "Tom"}, {$set: {age: 20}})


删除name字段为Tom的document的age为25的字段
db.students.update({name: "Tom"}, {$unset: {age: 25}}


 给name字段为Tom的document增加一个字段sex且值为男
db.students.update({name: "Tom"}, {$inc: {sex: "男"}})

createUser()方法

# 创建用户
db.createUser({user:"root",pwd:"123456", roles: [{ role: "root", db: "admin" }]}); 
# 删除用户
db.system.users.remove({user:"root"});

角色种类说明
Mongodb Index 介绍

db.students.createIndex({name: 1})
给name字段创建索引, 1为指定按升序创建索引,如果你想按降序来创建索引指定为-1即可
# 查看索引
db.students.getIndexes()
# 查看是否使用到了索引(由于MongoDB调优)
db.students.find({"name": "Angle"}).explain()  # winningPlan的stage为fetch,非COLLSCAN(字段扫描)
# 删除索引
db.student.dropIndex("name_1")
# 给name字段创建一个唯一键索引,那么再给students表增加一条行document,且name与之前存在的document的name值相同,那么就会报错, 如:增加一条document
db.students.createIndex({name: 1}, {unique: true})
db.students.insert({name: "Angle"})
报错的信息如下:
WriteResult({
        "nInserted" : 0,
        "writeError" : {
                "code" : 11000,
                "errmsg" : "insertDocument :: caused by :: 11000 E11000 duplicate key error index: test.students.$name_1  dup key: { : \"Angle\" }"
        }
})

参考: https://www.9xkd.com/user/plan-view.html?id=1609408774

推荐阅读:
  1. 为什么使用MongoDB?
  2. mongodb使用

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

mongodb 使用

上一篇:mariadb 5.5.50 default-storage-engine

下一篇:在微信小程序里自动获得当前手机所在的经纬度并转换成地址

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》