python 操作mongo

1.  导包:

    import pymongo

2.  建立连接

    client = pymongo.MongoClient("127.0.0.1",27017)

3.  获取数据库

    db = client["test1"]

4.  获取集合

    col = db["t2"]

5.  插入数据:

    col.insert_one({ 'name':'aa' , 'age':2 })    插入一条数据

    插入多条数据:

    li = [

      {'name':"PA",'age':1},

      {'name': "Pb", 'age': 2},

      {'name':"Pc",'age':2}

      ]

    col.insert_many(li)  

6.  修改数据

5  测试

    data  = col.find()    一个对象

    data2 = col.find_one()  一条数据

猜你喜欢

转载自www.cnblogs.com/cxhzy/p/9953513.html