【数据库的学习】mongodb的基础使用与python接口

mongodb :键值对形式,适用于爬虫,百度地图等;文档-类似与字典,由文档组成集合。
分布式:

  1. 可靠性
  2. 可扩展性
  3. 资源共享
  4. 更快的速度

命令

  1. 进入: mongo

     ~$ mongo
     MongoDB shell version v3.6.3
     ……
     2020-02-22T16:04:39.509+0800 ICONTROL  [initandlisten] 
    

库操作

  1. 显示所有数据库: show dbs

     > show dbs
     admin     0.000GB
     config    0.000GB
     local     0.000GB
    
  2. 使用数据库: use databases,有则使用,无则创建,若数据库里没有集合,则dbs不会显示数据库

     > use python
     switched to db python
    
  3. 查看当前所在数据库: db

     > db
     python
    
  4. 删除当前数据库: db.dropDatabase() 区分大小写,驼峰标识

集合操作

  1. 创建集合:db.createCollection(‘name’)

     > db.createCollection('stu')
     { "ok" : 1 }
    
  2. 显示当前数据库的所有集合:show collections

     > show collections
     stu
    
  3. 删除集合:db.集合名字.drop()

文档操作

  1. 往集合里插入文档:db.集合名字.insert({‘key’:value,‘key’:value}) _id 是自动添加的唯一性标识,也可手动添加{’_id’:123}

     > db.stu.insert({'_id':123,'name':'zhen','age':21})
     WriteResult({ "nInserted" : 1 })
    
  2. 插入多行数据:db.集合名字 .insert([{},{},{}])

    > db.stu.insert([{'name':'lili','age':11},{'name':'ilil','age':21}])
    BulkWriteResult({
    	"writeErrors" : [ ],
    	"writeConcernErrors" : [ ],
    	"nInserted" : 2,
    	"nUpserted" : 0,
    	"nMatched" : 0,
    	"nModified" : 0,
    	"nRemoved" : 0,
    	"upserted" : [ ]
    })
    
  1. 查询集合内容:db.集合名字.find()

    > db.stu.find()
    { "_id" : ObjectId("5e53c343717845eaf7f530cc"), "name" : "li", "age" : 22 }
    
  2. 美化显示查询集合内容:db.集合名字.find().pretty()

    > db.stu.find().pretty()
    { "_id" : ObjectId("5e53c343717845eaf7f530cc"), "name" : "li", "age" : 22 }
    { "_id" : 123, "name" : "zhen", "age" : 21 }
    {
    	"_id" : ObjectId("5e53c45e717845eaf7f530cd"),
    	"name" : "lili",
    	"age" : 11
    }
    
  3. 查询满足条件的:db.集合名字.find({‘key’:value}) key可以不用加引号

    > db.stu.find({name:'li'})
    
  4. 运算符

    不等于:$ne
    大于:$gt
    小于:$lt
    大于等于:$gte
    小于等于:$lte
    db.集合名字.find({key:{$gt:20}})
    
  5. $and 查询:db.集合名字.find({$and:[{key:value},{}]})

  6. $or 查询:db.集合名字.find({$or:[{key:value},{}]})

    db.stu.find({$or:[{name:'li'},{age:{$gt:11}}]})
    
  7. $or 里嵌套and 查询:db.集合名字.find({KaTeX parse error: Expected '}', got 'EOF' at end of input: or:[{and:[{key:value},{key:value}]},{$and:[{key:value},{key:value}]}]})

  1. 修改 :db.集合名字.update({修改的条件},{修改的值}),满足条件的多个只修改一个;属于覆盖修改,加参数$set 只修改非覆盖

    覆盖:db.stu.update({age:22},{name:'xxx'})
    修改:db.stu.update({age:22},{$set:{name:'xxx'}})
    全部修改:db.stu.update({age:22},{$set:{name:'xxx'}},{multi:true})
    
  1. 删除文档:db.集合.remove({key:value}) 不给条件,只有{}会删除所有,只删除符合条件的第一条参数:justOne:true

    只删除一条:db.stu.remove({age:22},{justOne:true})
    

pycharm 操作mongo

  1. 导入库

     import pymongo
    
  2. 建立连接——pymongo.MongoClient()

     clinent = pymongo.MongoClient()
    
  3. 连接数据库——clinent[‘python37’]

     clinent['python37']
    
  4. 连接集合——db[‘student’]

     my_collection = db['student']
    
  5. 集合操作——增删改查

     增一条——insert_one()
     my_collection.insert_one({'name':'li','age':13,'sex':'f'})
     增多条——insert_many()
     my_collection.insert_many([
      {},
      {},
      {}
           ])
     查询——find_one()
     res = my_collection.find_one() # 查询一条
     res = my_collection.find()  #查询全部,返回的是可迭代的结果,也可以list(res)强转为列表
     修改——update_one()
    
     my_collection.update_one({'name':'value'},	{'$set':{'age':20}})  参数要用字符串的形式传递
     删除——delete_one()  注意不是remove
     res = my_collection.delete_one({'name':'value'})
    
发布了5 篇原创文章 · 获赞 0 · 访问量 163

猜你喜欢

转载自blog.csdn.net/weixin_41655783/article/details/104484445