python执行shell脚本、执行mongodb_Mongo shell 的基本操作和 Python shell 中使用 MongoDB...
Mongo shell 的基本操作
MongoDB 分四級存儲:
1、數據庫 db
2、文檔集合 collections(相當于 MySQL 的數據庫表)
3、文檔 document(相當于 MySQL 數據庫表里的一條數據)
4、字段
首先啟動 mongo 服務:
shiyanlou:~/ $ sudo service mongod start
* Starting database mongod
shiyanlou:~/ $
執行 mongo 或 sudo mongo 進入到 mongo shell ,
執行 use 即可切換到此數據庫,
若此數據庫不存在,該命令并不會創建這個數據庫,
當執行 db..insertOne 創建了一條數據時,
該數據庫和文檔集合才真正創建:
shiyanlou:~/ $ mongo
MongoDB shell version v3.4.7
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.7
Server has startup warnings:
2018-02-19T12:47:00.461+0800 I STORAGE [initandlisten]
2018-02-19T12:47:00.461+0800 I STORAGE [initandlisten]
... ...
2018-02-19T12:47:00.816+0800 I CONTROL [initandlisten]
2018-02-19T12:47:00.816+0800 I CONTROL [initandlisten]
2018-02-19T12:47:00.816+0800 I CONTROL [initandlisten]
> show databases
admin 0.000GB
local 0.000GB
> use shiyanlou
switched to db shiyanlou
> show databases
admin 0.000GB
local 0.000GB
> db.user.insertOne(
... {name: 'Kobe', age: 39, addr: ['Los', 'Tor']}
... )
{
"acknowledged" : true,
"insertedId" : ObjectId("5a8a6511f04f005f97780c8c")
}
> show databases
admin 0.000GB
local 0.000GB
shiyanlou 0.000GB
>
插入多條數據:
> db.user.insertMany([
... {name: 'Nash', age: 43, addr: ['Pho', 'Los']},
... {name: 'Jame', age: 33, addr: ['Mia', 'Cle']}
... ])
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("5a8a677ff04f005f97780c8d"),
ObjectId("5a8a677ff04f005f97780c8e")
]
}
>
查詢數據可以使用 db.collection.find 方法,可以指定查詢過濾條件:
> db.user.find() # findOne() 可以查詢第一條數據
{ "_id" : ObjectId("5a8a6511f04f005f97780c8c"), "name" : "Kobe", "age" : 39, "addr" : [ "Los", "Tor" ] }
{ "_id" : ObjectId("5a8a677ff04f005f97780c8d"), "name" : "Nash", "age" : 43, "addr" : [ "Pho", "Los" ] }
{ "_id" : ObjectId("5a8a677ff04f005f97780c8e"), "name" : "Jame", "age" : 33, "addr" : [ "Mia", "Cle" ] }
> db.user.find({name: 'Kobe'})
{ "_id" : ObjectId("5a8a6511f04f005f97780c8c"), "name" : "Kobe", "age" : 39, "addr" : [ "Los", "Tor" ] }
> db.user.find({age: {$gt: 35}}) # gt 表示大于,lt 表示小于
{ "_id" : ObjectId("5a8a6511f04f005f97780c8c"), "name" : "Kobe", "age" : 39, "addr" : [ "Los", "Tor" ] }
{ "_id" : ObjectId("5a8a677ff04f005f97780c8d"), "name" : "Nash", "age" : 43, "addr" : [ "Pho", "Los" ] }
>
更新數據主要通過 db.user.updateOne 或者 db.user.updateMany 方法,
前者更新一條記錄,后者更新多條記錄:
> db.user.updateOne(
... {name: 'Nash'},
... {$set: {addr: ['Pho', 'Dal', 'Los']}}
... )
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> db.user.find({name: 'Nash'})
{ "_id" : ObjectId("5a8a677ff04f005f97780c8d"), "name" : "Nash", "age" : 43, "addr" : [ "Pho", "Dal", "Los" ] }
>
刪除數據通過 db.user.deleteOne(默認刪除第一條符合條件的數據)
或db.user.deleteMany(默認刪除全部符合條件的數據)方法:
> db.user.find()
{ "_id" : ObjectId("5a8a6511f04f005f97780c8c"), "name" : "Kobe", "age" : 39, "addr" : [ "Los", "Tor" ] }
{ "_id" : ObjectId("5a8a677ff04f005f97780c8d"), "name" : "Nash", "age" : 43, "addr" : [ "Pho", "Dal", "Los" ] }
{ "_id" : ObjectId("5a8a677ff04f005f97780c8e"), "name" : "Jame", "age" : 33, "addr" : [ "Mia", "Cle" ] }
> db.user.deleteMany({addr: 'Los'})
{ "acknowledged" : true, "deletedCount" : 2 }
> db.user.find()
{ "_id" : ObjectId("5a8a677ff04f005f97780c8e"), "name" : "Jame", "age" : 33, "addr" : [ "Mia", "Cle" ] }
>
刪除數據還可以用 db.collection.remove 方法,
它默認刪除所有符合條件的數據,此命令可以刪除全部數據:
> db.user.find()
{ "_id" : ObjectId("5a8a6d13f04f005f97780c94"), "name" : "Kobe", "age" : 33 }
{ "_id" : ObjectId("5a8a6d13f04f005f97780c95"), "name" : "Nash", "age" : 33 }
> db.user.remove({}) # 刪除全部數據
WriteResult({ "nRemoved" : 2 })
> db.user.find()
>
在數據庫中刪除文檔集合(也就是數據庫表):
> show collections # 查看該數據庫中的文檔集合
user
> db.user.drop()
true
> show collections
> show databases # 如果刪除的是該數據庫最后一個 collection ,那么這個數據庫也就不存在了
admin 0.000GB
local 0.000GB
>
在某數據庫中刪除此數據庫:
> show databases
admin 0.000GB
local 0.000GB
shiyanlou 0.000GB
> use shiyanlou
switched to db shiyanlou
> db.dropDatabase()
{ "dropped" : "shiyanlou", "ok" : 1 }
> show databases
admin 0.000GB
local 0.000GB
>
Python shell 中使用 MongoDB
在 Python 中訪問 MongoDB 數據庫,主要通過 PyMongo 軟件包。
該軟件包包含一個 MongoClient 對象,可以用于建立 MongoDB 客戶端。
在 ipython 中輸入下面的示例代碼,創建客戶端:
shiyanlou:~/ $ ipython
Python 3.5.3 (default, Apr 22 2017, 00:00:00)
Type 'copyright', 'credits' or 'license' for more information
IPython 6.2.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: from pymongo import MongoClient
In [2]: client = MongoClient('127.0.0.1', 27017)
In [3]: db = client.shiyanlou # db 就是指 shiyanlou 數據庫
In [4]:
查詢數據:
# 默認查詢全部符合條件的數據
In [4]: for i in db.user.find():
...: print(i)
...:
{'age': 33.0, '_id': ObjectId('5a8a76f597407582a38e58b0'), 'name': 'Kobe', 'addr': 'Los'}
{'age': 32.0, '_id': ObjectId('5a8a76f597407582a38e58b1'), 'name': 'Jame', 'addr': 'Cle'}
{'age': 31.0, '_id': ObjectId('5a8a76f597407582a38e58b2'), 'name': 'Wade', 'addr': 'Mim'}
In [5]:
插入數據:
# 插入一條數據
In [6]: db.user.insert_one(
...: {'name':'Irving', 'age': 29, 'addr': 'Bos'}
...: )
Out[6]:
# 查詢一條數據
In [7]: db.user.find_one({'name':'Irving'})
Out[7]:
{'_id': ObjectId('5a8a77cbf411f101fd16a7ef'),
'addr': 'Bos',
'age': 29,
'name': 'Irving'}
In [8]:
更新數據:
In [11]: db.user.find_one({'name': 'Kobe'})
Out[11]:
{'_id': ObjectId('5a8a76f597407582a38e58b0'),
'addr': 'Los',
'age': 33.0,
'name': 'Kobe'}
# 更新一條數據
In [12]: db.user.update_one({'name':'Kobe'}, {'$set': {'age': 39}})
Out[12]:
In [13]: db.user.find_one({'name': 'Kobe'})
Out[13]:
{'_id': ObjectId('5a8a76f597407582a38e58b0'),
'addr': 'Los',
'age': 39,
'name': 'Kobe'}
In [14]:
刪除一條數據:
In [13]: db.user.find_one({'name': 'Kobe'})
Out[13]:
{'_id': ObjectId('5a8a76f597407582a38e58b0'),
'addr': 'Los',
'age': 39,
'name': 'Kobe'}
In [14]: db.user.delete_one({'name': 'Kobe'})
Out[14]:
In [15]: db.user.find_one({'name': 'Kobe'})
In [16]:
總結
以上是生活随笔為你收集整理的python执行shell脚本、执行mongodb_Mongo shell 的基本操作和 Python shell 中使用 MongoDB...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 田螺汤的功效与作用、禁忌和食用方法
- 下一篇: tooltip trigger怎么改气泡