引用pymongo
>>>importpymongo
建立連線connection
>>> import pymongo
>>> conn =pymongo.connection('localhost',27017)
或》 frompymongo import connection
>>> conn =connection('localhost',27017)
建立connection時,指定host及port引數
>>> import pymongo
>>> conn =pymongo.connection(host='127.0.0.1',port=27017)
連線資料庫
>>> db =conn.chatroom
或》 db =conn['chatroom']
連線聚集
>>> account = db.account
或 >>> account =db["account"]
檢視全部聚集名稱
>>> db.collection_names()
檢視聚集的一條記錄
>>>db.account.find_one()
>>>db.account.find_one()
檢視聚集的字段
>>>db.account.find_one({},)
>>>db.account.find_one({},)
檢視聚集的多條記錄
>>>foritem indb.account.find():
item
>>>foritem indb.account.find():
item["username"]
檢視聚集的記錄統計
>>>db.account.find().count()
>>>db.account.find().count()
聚集查詢結果排序
>>>db.account.find().sort("username") --預設為公升序
>>>db.account.find().sort("username",pymongo.ascending) --公升序
>>>db.account.find().sort("username",pymongo.descending) --降序
聚集查詢結果多列排序
>>>db.account.find().sort([("username",pymongo.ascending),("email",pymongo.descending)])
新增記錄
>>>db.account.insert()
修改記錄
>>>db.account.update(,})
刪除記錄
>>>db.account.remove() --全部刪除
>>>db.test.remove()
PyMongo基本使用
引用pymongo import pymongo 建立連線connection import pymongo conn pymongo.connection localhost 27017 或import conn connection localhost 27017 建立connection時,指...
PyMongo基本使用
引用pymongo import pymongo 建立連線connection import pymongo conn pymongo.connection localhost 27017 或 from pymongo import connection conn connection localh...
PyMongo基本使用
引用pymongo import pymongo 建立連線connection import pymongo conn pymongo.connection localhost 27017 或 from pymongo import connection conn connection localh...