Python中的MongoDB基本操作:連接、查詢實(shí)例
MongoDB是一個(gè)基于分布式文件存儲(chǔ)的數(shù)據(jù)庫(kù)。由C++語(yǔ)言編寫。旨在為WEB應(yīng)用提供可護(hù)展的高性能數(shù)據(jù)存儲(chǔ)解決方案。它的特點(diǎn)是高性能、易部署、易使用,存儲(chǔ)數(shù)據(jù)非常方便。
MongoDB 簡(jiǎn)單使用
聯(lián)接數(shù)據(jù)庫(kù)
In [1]: import pymongo
In [2]: from pymongo import Connection
In [3]: connection = Connection('192.168.1.3', 27017) //創(chuàng)建聯(lián)接
Connection 相關(guān)參數(shù)
Connection([host='localhost'[, port=27017[, pool_size=None[, auto_start_request=None[, timeout=None[, slave_okay=False[, network_timeout=None[, document_class=dict[, tz_aware=True]]]]]]]]])
數(shù)據(jù)庫(kù)操作
In [9]: c.database_names() //列出所有數(shù)據(jù)庫(kù)名稱
Out[9]: [u'test', u'admin', u'yuhen', u'sms', u'local']
In [10]: c.server_info() //查看服務(wù)器相關(guān)信息
Out[10]:
{u'bits': 64,
u'gitVersion': u'nogitversion',
u'ok': 1.0,
u'sysInfo': u'Linux yellow 2.6.24-27-server #1 SMP Fri Mar 12 01:23:09 UTC 2010 x86_64 BOOST_LIB_VERSION=1_40',
u'version': u'1.2.2'}
In [16]: db = c['test'] //選擇數(shù)據(jù)庫(kù)
In [17]: db.collection_names() //列出當(dāng)前數(shù)據(jù)庫(kù)中所有集合名稱
Out[17]: [u'system.indexes', u'fs.files', u'fs.chunks', u'test_gao']
In [23]: db.connection //查看聯(lián)接信息
Out[23]: Connection('192.168.1.3', 27017)
In [24]: db.create_collection('test_abeen') //創(chuàng)建新集合
Out[24]: Collection(Database(Connection('192.168.1.3', 27017), u'test'), u'test_abeen')
In [25]: db.last_status() //查看上次操作狀態(tài)
Out[25]: {u'err': None, u'n': 0, u'ok': 1.0}
In [26]: db.name //查看當(dāng)前數(shù)據(jù)庫(kù)名稱
Out[26]: u'test'
In [27]: db.profiling_info() //查看配置信息
Out[27]: []
In [28]: db.profiling_level()
Out[28]: 0.0
集合操作
In [31]: db.collection_names() //查看當(dāng)前數(shù)據(jù)庫(kù)所有集合名稱
Out[31]:
[u'system.indexes',
u'fs.files',
u'fs.chunks',
u'test_gao',
u'system.users',
u'test_abeen']
In [32]: c = db.test_abeen //選擇集合
In [33]: c.name //查看當(dāng)前集合名稱
Out[33]: u'test_abeen'
In [35]: c.full_name //查看當(dāng)前集合全名
Out[35]: u'test.test_abeen'
In [36]: c.database //查看當(dāng)前集合數(shù)據(jù)庫(kù)相關(guān)信息
Out[36]: Database(Connection('192.168.1.3', 27017), u'test')
In [38]: post = {"author":"Mike","text":"this is a test by abeen"}
In [39]: posts = db.posts
In [40]: posts.insert(post) //向數(shù)據(jù)庫(kù)集合插入文檔,默認(rèn)創(chuàng)建集合
Out[40]: ObjectId('4c358492421aa91e70000000')
In [41]: db.collection_names() //顯示所有集合名稱
Out[41]:
[u'system.indexes',
u'fs.files',
u'fs.chunks',
u'test_gao',
u'system.users',
u'test_abeen',
u'posts']
In [42]: posts.find_one() //從集合查找信息
Out[42]:
{u'_id': ObjectId('4c358492421aa91e70000000'),
u'author': u'Mike',
u'text': u'this is a test by abeen'}
In [52]: p.update({"author":"Mike"},{"$set":{"author":"abeen","text":"this is a test by abeen shan shan"}})//更新集合文檔信息
In [55]: list(p.find())
Out[55]:
[{u'_id': ObjectId('4c358492421aa91e70000000'),
u'author': u'abeen',
u'text': u'this is a test by abeen shan shan'}]
In [96]: list(posts.find())
Out[96]:
[{u'_id': ObjectId('4c358492421aa91e70000000'),
u'author': u'Mike',
u'text': u'this is a test by abeen'},
{u'_id': ObjectId('4c358ad4421aa91e70000002'), u'a': u'aa', u'b': u'bb'},
{u'_id': ObjectId('4c358ad9421aa91e70000003'), u'a': u'aa', u'b': u'bb'},
{u'_id': ObjectId('4c358abb421aa91e70000001'),
u'a': u'abeen',
u'b': u'this bb is updated'}]
In [97]: posts.remove({"a":"abeen"}) //刪除符合條件的文檔
In [98]: list(posts.find())
Out[98]:
[{u'_id': ObjectId('4c358492421aa91e70000000'),
u'author': u'Mike',
u'text': u'this is a test by abeen'},
{u'_id': ObjectId('4c358ad4421aa91e70000002'), u'a': u'aa', u'b': u'bb'},
{u'_id': ObjectId('4c358ad9421aa91e70000003'), u'a': u'aa', u'b': u'bb'}]
In [102]: db.collection_names()
Out[102]:
[u'system.indexes',
u'fs.files',
u'fs.chunks',
u'test_gao',
u'system.users',
u'test_abeen',
u'posts',
u'doc_abeen']
In [104]: db.drop_collection("doc_abeen") //刪除集合
In [105]: db.collection_names()
Out[105]:
[u'system.indexes',
u'fs.files',
u'fs.chunks',
u'test_gao',
u'system.users',
u'test_abeen',
u'posts']
代碼
In [113]: result = db.posts.find({"a":"aa"})//查找
In [114]: type(result)
Out[114]: <class 'pymongo.cursor.Cursor'>
In [119]: list(result)
Out[119]:
[{u'_id': ObjectId('4c358ad4421aa91e70000002'), u'a': u'aa', u'b': u'bb'},
{u'_id': ObjectId('4c358ad9421aa91e70000003'), u'a': u'aa', u'b': u'bb'}]
find格式
find([spec=None[, fields=None[, skip=0[, limit=0[, timeout=True[, snapshot=False[, tailable=False[, sort=None[, max_scan=None[, as_class=None[, **kwargs]]]]]]]]]]])
代碼
In [120]: db.posts.count()//當(dāng)前集合文檔數(shù)
Out[120]: 3
In [121]: type(db.posts)
Out[121]: <class 'pymongo.collection.Collection'>
In [138]: posts.rename('test_abeen')//重命名當(dāng)前集合
In [139]: db.collection_names()
Out[139]:
[u'system.indexes',
u'fs.files',
u'fs.chunks',
u'test_gao',
u'system.users',
u'test_abeen']
In [151]: for post in c.find({"a":"aa"}).sort("a"): //查詢并排序列
post
Out[152]: {u'_id': ObjectId('4c358ad4421aa91e70000002'), u'a': u'aa', u'b': u'bb'}
Out[152]: {u'_id': ObjectId('4c358ad9421aa91e70000003'), u'a': u'aa', u'b': u'bb'}
> db.foo.insert( { x : 1, y : 1 } )
> db.foo.insert( { x : 2, y : "string" } )
> db.foo.insert( { x : 3, y : null } )
> db.foo.insert( { x : 4 } )
// Query #1 y 為null或不存在
> db.foo.find( { "y" : null } )
{ "_id" : ObjectId("4dc1975312c677fc83b5629f"), "x" : 3, "y" : null }
{ "_id" : ObjectId("4dc1975a12c677fc83b562a0"), "x" : 4 }
// Query #2 y為null的值
> db.foo.find( { "y" : { $type : 10 } } )
{ "_id" : ObjectId("4dc1975312c677fc83b5629f"), "x" : 3, "y" : null }
// Query #3 y不存在的結(jié)果
> db.foo.find( { "y" : { $exists : false } } )
{ "_id" : ObjectId("4dc1975a12c677fc83b562a0"), "x" : 4 }
- python連接mongodb操作數(shù)據(jù)示例(mongodb數(shù)據(jù)庫(kù)配置類)
- Python的MongoDB模塊PyMongo操作方法集錦
- 在Python中使用mongoengine操作MongoDB教程
- 使用Python腳本操作MongoDB的教程
- python操作MongoDB基礎(chǔ)知識(shí)
- Python常見MongoDB數(shù)據(jù)庫(kù)操作實(shí)例總結(jié)
- 詳解Python3操作Mongodb簡(jiǎn)明易懂教程
- Python操作mongodb數(shù)據(jù)庫(kù)進(jìn)行模糊查詢操作示例
- Python 操作 MongoDB 講解詳細(xì)
- Python操作MongoDB的實(shí)現(xiàn)示例
相關(guān)文章
django 將自帶的數(shù)據(jù)庫(kù)sqlite3改成mysql實(shí)例
這篇文章主要介紹了django 將自帶的數(shù)據(jù)庫(kù)sqlite3改成mysql實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2020-07-07基于python3抓取pinpoint應(yīng)用信息入庫(kù)
這篇文章主要介紹了基于python3抓取pinpoint應(yīng)用信息入庫(kù),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01Python中使用json.load()和json.loads()加載json數(shù)據(jù)的方法實(shí)例
在python編程中,我們經(jīng)常要用到j(luò)son對(duì)象作為數(shù)據(jù)交換格式,下面這篇文章主要給大家介紹了關(guān)于Python中使用json.load()和json.loads()加載json數(shù)據(jù)的方法實(shí)例,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08使用python實(shí)現(xiàn)微信小程序自動(dòng)簽到功能
這篇文章主要介紹了使用python實(shí)現(xiàn)微信小程序自動(dòng)簽到功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04