>> import pymongo >>> client=pymongo.MongoClient ( localhost ,27017) ..."/>
您好,登錄后才能下訂單哦!
本文實例講述了Python操作MongoDB數(shù)據(jù)庫的方法。分享給大家供大家參考,具體如下:
>>> import pymongo >>> client=pymongo.MongoClient ('localhost',27017) >>> db=client.students >>> db.collection_names() ['students'] >>> students=db.students >>> students.find() <pymongo.cursor.Cursor object at 0x0000017A74305FD0> >>> for item in students.find(): print(item) {'_id': ObjectId('59394a87ae09c56bd9c1d375'), 'name': 'zhangsan', 'age': 18.0, 'sex': 'male'} >>> wangwu={'name':'wangwu','age':20,'sex':'male'} >>> students.insert(wangwu) ObjectId('593a7c5fedb5a1abeb757052') >>> for item in students.find({'name':'wangwu'}): print(item) {'_id': ObjectId('593a7c5fedb5a1abeb757052'), 'name': 'wangwu', 'age': 20, 'sex': 'male'} >>> students.find_one() {'_id': ObjectId('59394a87ae09c56bd9c1d375'), 'name': 'zhangsan', 'age': 18.0, 'sex': 'male'} >>> students.find_one({'name':'wangwu'}) {'_id': ObjectId('593a7c5fedb5a1abeb757052'), 'name': 'wangwu', 'age': 20, 'sex': 'male'} >>> students.find().count() 2 >>> students.remove({'name':'wangwu'}) {'ok': 1, 'n': 1} >>> for item in students.find(): print(item) {'_id': ObjectId('59394a87ae09c56bd9c1d375'), 'name': 'zhangsan', 'age': 18.0, 'sex': 'male'} >>> students.find().count() 1 >>> students.create_index([('name',pymongo.ASCENDING)]) 'name_1' >>> students.update({'name':'zhangsan'},{'$set':{'age':25}}) {'ok': 1, 'nModified': 1, 'n': 1, 'updatedExisting': True} >>> students.find_one() {'_id': ObjectId('59394a87ae09c56bd9c1d375'), 'name': 'zhangsan', 'age': 25, 'sex': 'male'} >>> students.update({'age':25},{'$set':{'sex':'Female'}}) {'ok': 1, 'nModified': 1, 'n': 1, 'updatedExisting': True} >>> students.remove() {'ok': 1, 'n': 1} >>> students.find().count() 0 >>> zhangsan={'name':'zhangsan','age':25,'sex':'Male'} >>> lisi={'name':'lisi','age':21,'sex':'Male'} >>> wangwu={'name':'wangwu','age':22,'sex':'Female'} >>> students.insert_many([zhangsan,lisi,wangwu]) <pymongo.results.InsertManyResult object at 0x0000017A749FC5E8> >>> for item in students.find().sort('name',pymongo.ASCENDING): print(item) {'_id': ObjectId('593a806bedb5a1abeb757054'), 'name': 'lisi', 'age': 21, 'sex': 'Male'} {'_id': ObjectId('593a806bedb5a1abeb757055'), 'name': 'wangwu', 'age': 22, 'sex': 'Female'} {'_id': ObjectId('593a806bedb5a1abeb757053'), 'name': 'zhangsan', 'age': 25, 'sex': 'Male'} >>> for item in students.find().sort([('sex',pymongo.DESCENDING),('name',pymongo.ASCENDING)]): print(item) {'_id': ObjectId('593a806bedb5a1abeb757054'), 'name': 'lisi', 'age': 21, 'sex': 'Male'} {'_id': ObjectId('593a806bedb5a1abeb757053'), 'name': 'zhangsan', 'age': 25, 'sex': 'Male'} {'_id': ObjectId('593a806bedb5a1abeb757055'), 'name': 'wangwu', 'age': 22, 'sex': 'Female'} >>>
更多關于Python相關內容感興趣的讀者可查看本站專題:《Python常見數(shù)據(jù)庫操作技巧匯總》、《Python數(shù)學運算技巧總結》、《Python數(shù)據(jù)結構與算法教程》、《Python函數(shù)使用技巧總結》、《Python字符串操作技巧匯總》、《Python入門與進階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設計有所幫助。
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。