mongoDB

2022/3/7 2:15:27

本文主要是介绍mongoDB,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

MongoDB

01 认识MongoDB

MongoDB简介
       基于分布式文件存储的开源数据库系统。旨在为WEB应用提供可扩展的高性能数据存储解决方案。将数据存储为一个文档,文档类似于 Json格式,MongoDB是非关系型数据库,Json格式和字典格式很类似
​
     {
          name:"aa" , 
           age:100,
           address:{city:"cs",country:"china"}
       }

 

MongoDB数据模型

 

 

02 库、集合操作

库与表操作

进入数据库:     mongo                                 
​
退出:        exit
​
显示所有库:   show dbs                          
​
切换/创建数据库:use 数据库名称      
​
> use my_db
> switched to db my_db
​
创建集合:          db.createCollection('name')      #里面是字符串
​
> db.createCollection('student')
> { "ok" : 1 }
​
显示当前数据库的集合:  show collections      #只有里面有数据的库才能查到,如果里面没有数据 ,即便你已经创建了,你也show查不到
​
> show collections
> student
​
查看当前所在库:     db                                      
​
删除集合:        db.集合名称.drop()
​
删除库:            db.dropDatabase()     #进入以后才能删除,删除的也就是打开的那个库

 

03 文档(数据)操作

添加文档(数据):db.集合名称.insert(document)

添加一条文档 db.students.insert({name:'xiaoming',age:18})

db.students.insert({name:'xiaoming',age:18}) WriteResult({ "nInserted" : 1 }) db.students.insert({name:'qqq',age:22}) WriteResult({ "nInserted" : 1 }) db.students.insert({name:'aaa',age:55}) WriteResult({ "nInserted" : 1 }) db.students.insert({name:'zzz',age:23}) WriteResult({ "nInserted" : 1 }) db.students.insert({name:'www',age:18}) WriteResult({ "nInserted" : 1 }) db.students.insert({name:'sss',age:18}) WriteResult({ "nInserted" : 1 }) db.students.insert({name:'ddd',age:20}) WriteResult({ "nInserted" : 1 }) db.students.insert({name:'ccc',age:16}) WriteResult({ "nInserted" : 1 }) db.students.insert({name:'vvv',age:18}) WriteResult({ "nInserted" : 1 }) db.students.insert({name:'rrr',age:20}) WriteResult({ "nInserted" : 1 })

 

添加多条文档 db.students.insert([{name:'flz',age:20,sex:'男'},{name:'lanqiao',age:30,sex:'男'}])

db.students.insert([{name:'flz',age:20,sex:'男'},{name:'lanqiao',age:30,sex:'男'}]) BulkWriteResult({ "writeErrors" : [ ], "writeConcernErrors" : [ ], "nInserted" : 2, "nUpserted" : 0, "nMatched" : 0, "nModified" : 0, "nRemoved" : 0, "upserted" : [ ] })

查询文档(数据)

db.集合名称.find([conditions])

查看集合中全部数据:db.students.find() #这里会自动生成一些东西用来区分数据一样的

db.students.find() { "id" : ObjectId("6223572ecca46b2c9c9f42b0"), "name" : "xiaoming", "age" : 18 } { "id" : ObjectId("6223572ecca46b2c9c9f42b1"), "name" : "qqq", "age" : 22 } { "id" : ObjectId("6223572ecca46b2c9c9f42b2"), "name" : "aaa", "age" : 55 } { "id" : ObjectId("6223572ecca46b2c9c9f42b3"), "name" : "zzz", "age" : 23 } { "id" : ObjectId("6223572ecca46b2c9c9f42b4"), "name" : "www", "age" : 18 } { "id" : ObjectId("6223572ecca46b2c9c9f42b5"), "name" : "sss", "age" : 18 } { "id" : ObjectId("6223572ecca46b2c9c9f42b6"), "name" : "ddd", "age" : 20 } { "id" : ObjectId("6223572ecca46b2c9c9f42b7"), "name" : "ccc", "age" : 16 } { "id" : ObjectId("6223572ecca46b2c9c9f42b8"), "name" : "vvv", "age" : 18 } { "id" : ObjectId("6223572ecca46b2c9c9f42b9"), "name" : "rrr", "age" : 20 }

id也可以自己设置

db.students.insert({"name":'qi',id:1}) WriteResult({ "nInserted" : 1 }) db.students.find() { "id" : ObjectId("6223572ecca46b2c9c9f42b0"), "name" : "xiaoming", "age" : 18 } { "id" : ObjectId("6223572ecca46b2c9c9f42b1"), "name" : "qqq", "age" : 22 } { "id" : ObjectId("6223572ecca46b2c9c9f42b2"), "name" : "aaa", "age" : 55 } { "id" : ObjectId("6223572ecca46b2c9c9f42b3"), "name" : "zzz", "age" : 23 } { "id" : ObjectId("6223572ecca46b2c9c9f42b4"), "name" : "www", "age" : 18 } { "id" : ObjectId("6223572ecca46b2c9c9f42b5"), "name" : "sss", "age" : 18 } { "id" : ObjectId("6223572ecca46b2c9c9f42b6"), "name" : "ddd", "age" : 20 } { "id" : ObjectId("6223572ecca46b2c9c9f42b7"), "name" : "ccc", "age" : 16 } { "id" : ObjectId("6223572ecca46b2c9c9f42b8"), "name" : "vvv", "age" : 18 } { "id" : ObjectId("6223572ecca46b2c9c9f42b9"), "name" : "rrr", "age" : 20 } { "id" : 1, "name" : "qi" }

格式化显示:db.students.find().pretty()

查看满足条件的数据:db.students.find({name:'xiaoming'})

db.students.find({name:'xiaoming'}) { "_id" : ObjectId("6223572ecca46b2c9c9f42b0"), "name" : "xiaoming", "age" : 18 }

噩梦条件

and条件 {$and:[{expression1},{expression1},...]}

or条件 {$or:[{expression1},{expression1},...]}

and和or混用

db.table.find({$or:[{$and:[{sex:'女'},{age:18}]},{$and:[{sex:'男'},{age:{$gt:18}}]}]})

例:

db.students.find({$or:[{name:’xiaoming’},{age:18}]})

操作符 描述

$ne 不等于

$gt 大于

$lt 小于

$gte 大于等于

$lte 小于等于

例子:一写冒号就是字典,必须用{}括起来,操作符也必须用{}括起来

db.students.find({"age":{$gte:18}}) { "id" : ObjectId("6223572ecca46b2c9c9f42b0"), "name" : "xiaoming", "age" : 18 } { "id" : ObjectId("6223572ecca46b2c9c9f42b1"), "name" : "qqq", "age" : 22 } { "id" : ObjectId("6223572ecca46b2c9c9f42b2"), "name" : "aaa", "age" : 55 } { "id" : ObjectId("6223572ecca46b2c9c9f42b3"), "name" : "zzz", "age" : 23 } { "id" : ObjectId("6223572ecca46b2c9c9f42b4"), "name" : "www", "age" : 18 } { "id" : ObjectId("6223572ecca46b2c9c9f42b5"), "name" : "sss", "age" : 18 } { "id" : ObjectId("6223572ecca46b2c9c9f42b6"), "name" : "ddd", "age" : 20 } { "id" : ObjectId("6223572ecca46b2c9c9f42b8"), "name" : "vvv", "age" : 18 } { "_id" : ObjectId("6223572ecca46b2c9c9f42b9"), "name" : "rrr", "age" : 20 }

修改文档(数据)

条件+要修改的东西

db.集合名称.update(<query>,<update>,{multi:<boolean>})

修改一条数据:db.students.update({sex:'男'},{age:20}) #会覆盖掉

db.students.update({name:'aaa'},{age:88}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

db.students.find() { "id" : ObjectId("6223572ecca46b2c9c9f42b0"), "name" : "xiaoming", "age" : 18 } { "id" : ObjectId("6223572ecca46b2c9c9f42b1"), "name" : "qqq", "age" : 22 } { "id" : ObjectId("6223572ecca46b2c9c9f42b2"), "age" : 88 } { "id" : ObjectId("6223572ecca46b2c9c9f42b3"), "name" : "zzz", "age" : 23 } { "id" : ObjectId("6223572ecca46b2c9c9f42b4"), "name" : "www", "age" : 18 } { "id" : ObjectId("6223572ecca46b2c9c9f42b5"), "name" : "sss", "age" : 18 } { "id" : ObjectId("6223572ecca46b2c9c9f42b6"), "name" : "ddd", "age" : 20 } { "id" : ObjectId("6223572ecca46b2c9c9f42b7"), "name" : "ccc", "age" : 16 } { "id" : ObjectId("6223572ecca46b2c9c9f42b8"), "name" : "vvv", "age" : 18 } { "id" : ObjectId("6223572ecca46b2c9c9f42b9"), "name" : "rrr", "age" : 20 } { "_id" : 1, "name" : "qi" }

 

指定属性修改:{$set:{age:20}} #只修改其中一部分

db.table.update({name:'xiaoming'},{$set:{age:666,sex:'xx'}})

db.students.update({name:'www'},{$set:{age:60}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

db.students.find() { "id" : ObjectId("6223572ecca46b2c9c9f42b0"), "name" : "xiaoming", "age" : 18 } { "id" : ObjectId("6223572ecca46b2c9c9f42b1"), "name" : "qqq", "age" : 22 } { "id" : ObjectId("6223572ecca46b2c9c9f42b2"), "age" : 88 } { "id" : ObjectId("6223572ecca46b2c9c9f42b3"), "name" : "zzz", "age" : 23 } { "id" : ObjectId("6223572ecca46b2c9c9f42b4"), "name" : "www", "age" : 60 } { "id" : ObjectId("6223572ecca46b2c9c9f42b5"), "name" : "sss", "age" : 18 } { "id" : ObjectId("6223572ecca46b2c9c9f42b6"), "name" : "ddd", "age" : 20 } { "id" : ObjectId("6223572ecca46b2c9c9f42b7"), "name" : "ccc", "age" : 16 } { "id" : ObjectId("6223572ecca46b2c9c9f42b8"), "name" : "vvv", "age" : 18 } { "id" : ObjectId("6223572ecca46b2c9c9f42b9"), "name" : "rrr", "age" : 20 } { "_id" : 1, "name" : "qi" }

 

更新集合中所有满足条件的文档:{multi:true}

db.students.update({sex:'男'},{$set:{sex:'女'}},{multi:true})

db.students.update({sex:'男'},{$set:{sex:'女'}},{multi:true}) WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })

db.students.find() { "id" : ObjectId("6223572ecca46b2c9c9f42b0"), "name" : "xiaoming", "age" : 18 } { "id" : ObjectId("6223572ecca46b2c9c9f42b1"), "name" : "qqq", "age" : 22 } { "id" : ObjectId("6223572ecca46b2c9c9f42b2"), "age" : 88 } { "id" : ObjectId("6223572ecca46b2c9c9f42b3"), "name" : "zzz", "age" : 23 } { "id" : ObjectId("6223572ecca46b2c9c9f42b4"), "name" : "www", "age" : 60 } { "id" : ObjectId("6223572ecca46b2c9c9f42b5"), "name" : "sss", "age" : 18 } { "id" : ObjectId("6223572ecca46b2c9c9f42b6"), "name" : "ddd", "age" : 20 } { "id" : ObjectId("6223572ecca46b2c9c9f42b7"), "name" : "ccc", "age" : 16 } { "id" : ObjectId("6223572ecca46b2c9c9f42b8"), "name" : "vvv", "age" : 18 } { "id" : ObjectId("6223572ecca46b2c9c9f42b9"), "name" : "rrr", "age" : 20 } { "id" : 1, "name" : "qi" } { "id" : ObjectId("62236218cca46b2c9c9f42ba"), "name" : "flz", "age" : 20, "sex" : "女" } { "_id" : ObjectId("62236218cca46b2c9c9f42bb"), "name" : "lanqiao", "age" : 30, "sex" : "女" }

删除文档(数据)

db.集合名称.remove(<query>,{justOne:<boolean>})

删除集合中所有的文档:db.table.remove({})

删除集合中满足条件的所有文档 db.table.remove({sex:'男'})

只删除集合中满足条件的第一条文档:{justOne:true}

db.table.remove({sex:'男'},{justOne:true})

04 Python操作mongodb,Python与MogoDB交互

pymongo

安装 pip install pymongo

建立连接:pymongo.MongoClient()

指定数据库:db=client[数据库名]

指定集合:collection=db[集合名]

官方推荐

查找文档:find_one() 查找所有:find()

添加文档:insert_one 添加多条:insert_many()

删除文档:delete_one 删除多条:delete_many()

修改文档:update_one 修改多条:update_many()



这篇关于mongoDB的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程