zoukankan      html  css  js  c++  java
  • Python Mongo数据库

    目录

    1、mongo的安装
    2、mongo数据库操作
    3、Python与Mongo交互
    

    1. mongo的安装

    (将安装目录下的bin目录添加到系统环境变量中)
    # mongo的配置: 
    1.创建数据库文件及日志文件目录, 并将目录写入配置文件 
    2.在系统终端执行以下命令: mongod --bind_ip 127.0.0.1 --logpath "D:ProfessionalMongoDBlogmongodb.log" - -logappend --dbpath "D:ProfessionalMongoDBdb" --port 27017 --serviceName "MongoDB" --serviceDisplayName "MongoDB" --install 3.将MongoDB服务设为开机自启
    

    2. mongo数据库操作

    //常用操作: 
    // 1.查看数据库 
    show dbs 
    // 2.创建并使用数据库 
    use tst 
    // 3.查看当前工作的数据库 
    db
    // 4.创建集合并插入一条数据 
    db.goods.insert({"name":"辣条", "price":0.5}) 
    // 5.查看所有表 
    show tables 
    // 6.查询表中所有数据 
    db.goods.find() 
    // 7.删除表操作
    db.goods.drop() 
    // 8.删除数据库 
    db.dropDatabase()
    
    
    # 增加数据操作: 
    db.tablename.insert({dict}) 
    # 示例: 增加数据操作: 
    db.goods.insert({"name":"辣条", "price":0.5}) 
    db.goods.insert({"name":"辣条", "price":1}) 
    db.goods.insert({"name":"干脆面", "price":0.5}) db.goods.insertOne({"name":"单身狗粮", "price":4.5}) db.goods.insertMany([{"name":"小洋人", "price":3.5}, {"name":"麦香鸡块", "price":5.5}])
    
    # 查询数据操作:
    //1、查看数据库   show dbs只能查看有数据的数据库
    show dbs
    
    //2、创建并使用数据库
    use a1903
    
    //3、查看当前正在工作的数据库
    db
    
    //4、插入数据
    db.student.insert({'name':'胡歌','age':30})
    
    db.student.insertOne([{'name':'胡歌'}])
    db.student.insertMany([{'name':'鹿晗','age':27},{'name':'关晓彤','age':23}])
    db.student.insertMany([{'name':'刘国鑫','age':22},{'name':'庞明哲','age':20}])
    //5、查询表中数据
    db.student.find()
    db.student.find().limit(3)
    
    
    // 按条件查询
    // 1.等值查询
    db.student.find({'name':'晓彤'})
    
    // 2.非等值查询:大于$gt(great than),小于$lt(little than),大于等于$gte(great than equal),小于等于$lte(little than equal),不等于$ne
    db.student.find({age:{$gt:23}})
    db.student.find({'age':{$lt:23}})
    db.student.find({'age':{$ne:21}})
    
    // 3. and 与 or 
    db.student.find({'name':'晓彤','age':21})
    db.student.find({$or[{'name':'晓彤'},{'age':27}]})
      
    // and  a 与 b之间
    db.student.find({'age':{$gt:20},'age':{$lte:27}})
    db.student.find({'age':{$gt:20,$lte:27}})
     
    db.student.find({$or:[{'age':{$lt:23}},{'age':{$gt:25}}]})
    
    // 更新数据操作
    db.student.update({'name':'晓彤'},{$set{'age':18}})
    
    //6、查看表
    show tables
    
    //7、删除表
    db.student.drop()
    
    //8、删除库
    db.dropDatabase()
    
    //9、指定删除
    db.student.remove({'name':''})
    
    # 更新数据操作: 
    db.table.update({定位字典}, {指定修改的键值}) 
    # 示例:更新数据操作: 
    	db.goods.update({"price":0.5},{$set:{"price":5}}) 
    		# 参数中的第一个字典用于定位要修改的数据 
    		# 参数中的第二个字典是指定要更新已定位的数据 
    		# 第二个参数中的字典是指定要将哪个字段的修改为什么
    
    # 删除数据操作: 
    db.tablename.remove({定位字典})
    # 示例:删除数据操作: 
    db.goods.remove({"price":5})
    

    3. Python与Mongo交互

    # 导入模块 如果没安装先安装  pip install pymongo
    import pymongo 
    # 连接MongoDB数据库 
    conn = pymongo.MongoClient('localhost', 27017) 
    # 创建库或连接数据库
    db = conn.goods 
    # 创建表或连接表
    table = db.snacks 
    # 数据操作: 插入数据 
    table.insert(dict) 
    table.insert_one(dict)  ******************在py文件中看具体清空使用
    table.insert_many([dict1, dict2, dict3]) 
    # 数据操作: 查询数据 
    table.find_one({dict}) 
    # 返回一个字典形式数据 
    table.find() 
    # 返回一个mongo对象, 需要使用for循环遍历取值 
    table.find({dict}) # 同上
    
  • 相关阅读:
    【LeetCode】226. Invert Binary Tree
    【LeetCode】235. Lowest Common Ancestor of a Binary Search Tree
    【LeetCode】191. Number of 1 Bits
    【LeetCode】122. Best Time to Buy and Sell Stock II
    【LeetCode】100. Same Tree
    【LeetCode】237. Delete Node in a Linked List
    【LeetCode】136. Single Number
    【LeetCode】104. Maximum Depth of Binary Tree
    svn tree conflicts 解决方法
    sed详解
  • 原文地址:https://www.cnblogs.com/xinzaiyuan/p/12382244.html
Copyright © 2011-2022 走看看