zoukankan      html  css  js  c++  java
  • The Usage of Pymongo

    Install

    pymongo document

    install pymongo from the tar package download from website

    python setup.y install

    Usage

    # import
    from pymongo import MongoClient
    
    # Making a Connection with MongoClient
    
    client = MongoClient(host='127.0.0.1', port=27017)
    
    # use the MongoDB URI format:
    
    # client = MongoClient('mongodb://localhost:27017/')
    
    # getting a database
    db_mark = client['mark']
    
    # getting a collection
    c_student = db_mark.student
    
    # getting all of the documents in collection c_student
    students = c_student.find()
    for student in students:
        print(student)  
    

    Insert

    The three methods to insert data to collection, obviously, insert() method is deprecated.

    # Insert an iterable of documents
    stu.insert_many(students)
    
    # insert is deprecated. Use insert_one or insert_many instead
    stu.insert()
    
    # Insert a single document
    stu.insert_one()
    
    from pymongo import MongoClient
    
    client = MongoClient('mongodb://localhost:27017/')
    
    db = client['python']
    # stu = db.create_collection('stu')
    stu = db.stu
    
    students = [
        {'name': 'jackson', 'age': 17, 'gender': 1},
        {'name': 'amada', 'age': 19, 'gender': 0},
        {'name': 'micheal', 'age': 23, 'gender': 1},
        {'name': 'lucy', 'age': 16, 'gender': 0},
        {'name': 'happy', 'age': 12, 'gender': 1},
        {'name': 'java', 'age': 34, 'gender': 0},
        {'name': 'python', 'age': 21, 'gender': 1},
        {'name': 'ruby', 'age': 11, 'gender': 1},
    ]
    
    stu.insert_many(students)
    
    #stu.insert()
    #stu.insert_one()
    

    Delete

    stu.delete_many()  # justOne:False
    stu.delete_one()  # justOne:True
    

    Update

    stu.update()
    stu.update_many()
    stu.update_one()
    

    find()

    stu.find()
    stu.find_one()
    stu.find_one_and_replace()
    stu.find_one_and_delete()
    stu.find_one_and_update()
    # show total number 
    stu.count()
    

    aggregate()

    In [22]: s.aggregate([{'$project':{'_id':0}}])                                                                                            
    Out[22]: <pymongo.command_cursor.CommandCursor at 0x7efdda495a20>
    
    In [23]: ret=s.aggregate([{'$project':{'_id':0}}])                                                                                        
    
    In [24]: for x in ret: 
        ...:     print(x) 
        ...:                                                                                                                                  
    {'name': 'jack', 'age': 12, 'gender': 1}
    {'name': 'rose', 'age': 11, 'gender': 0}
    {'name': 'jackson', 'age': 17, 'gender': 1}
    {'name': 'amada', 'age': 19, 'gender': 0}
    {'name': 'micheal', 'age': 23, 'gender': 1}
    {'name': 'lucy', 'age': 16, 'gender': 0}
    {'name': 'happy', 'age': 12, 'gender': 1}
    

    End

    fatal method to serach data from mongodb database is use aggregate

  • 相关阅读:
    2.4 使用vue-cli创建项目/项目打包/发布
    2.3 vue-cli脚手架工具/nodejs
    2.2 vue的devtools、eslint检测问题
    2. es6扩展运算符
    文件json
    函数
    函数不固定参数
    监控日志,加入黑名单
    非空即真
    随机生成手机号,存入文件
  • 原文地址:https://www.cnblogs.com/endurance9/p/10291287.html
Copyright © 2011-2022 走看看