Skip to main content

mongodbshel​​l 是一个使在 python shell 中使用 MongoDB 变得容易的类

项目描述

mongodbshel​​l : 一个可以在 python shell 中轻松使用 MongoDB 的模块

Python shell 是 Python 开发人员与 MongoDB 交互的理想环境。然而,输出游标和与数据库的交互需要更多的样板,而不是方便。该mongodbshell包提供了一组方便的函数和对象,以允许通过 Python 解释器更轻松地与 MongoDB 交互。

安装

您可以使用 pip3 或 pipenv 安装软件。mongodbshell仅支持 Python 3 。

$ pip3 install mongodbshell

可以在阅读文档中找到完整的 API 文档集

使用 mongodbshel​​l

首先我们创建一个MongoDB对象。这是我们可以使用运行的所有命令的代理MongoDBShell

>>> client=mongodbshell.MongoDB()
>>> client
mongodbshell.MongoDB('test', 'test', 'mongodb://localhost:27017')

如您所见,一个MongoDB对象嵌入了默认数据库test和集合 test。我们还可以访问本机MongoClient对象。

每个MongoDB对象都有许多标准属性:

>>> client
mongodbshell.MongoDB('test', 'test', 'mongodb://localhost:27017')
>>> client.client
MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True)
>>> client.database
Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True), 'test')
>>> client.collection
Collection(Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True), 'test'), 'test')
>>> client.uri
'mongodb://localhost:27017'
>>>

还有一些最流行的操作的便利功能:

>>> client.is_master()
{'ismaster': True,
 'localTime': datetime.datetime(2019, 1, 16, 15, 15, 41, 87000),
 'logicalSessionTimeoutMinutes': 30,
 'maxBsonObjectSize': 16777216,
 'maxMessageSizeBytes': 48000000,
 'maxWireVersion': 7,
 'maxWriteBatchSize': 100000,
 'minWireVersion': 0,
 'ok': 1.0,
 'readOnly': False}
 
>>> mongo_client.insert_one({"name" : "Joe Drumgoole", "twitter_handle" : "@jdrumgoole"})
ObjectId('5c3f4f2fc3b498d6674b08f0')
>>> mongo_client.find_one( {"name" : "Joe Drumgoole"})
1    {'_id': ObjectId('5c3f4b04c3b498d4a1c6ce22'),
2     'name': 'Joe Drumgoole',
3     'twitter_handle': '@jdrumgoole'}

输出行号

默认情况下,行号被添加到输出中。line_numbers您可以通过将标志设置为 false 来关闭行号。

>>> client.insert_one({"name" : "Joe Drumgoole", "twitter_handle" : "@jdrumgoole"})
ObjectId('5c3f4f2fc3b498d6674b08f0')
>>> client.find_one( {"name" : "Joe Drumgoole"})
1    {'_id': ObjectId('5c3f4b04c3b498d4a1c6ce22'),
2     'name': 'Joe Drumgoole',
3     'twitter_handle': '@jdrumgoole'}
>>> client.line_numbers = False                      # Turn off line numbers
>>> client.find_one( {"name" : "Joe Drumgoole"})
{'_id': ObjectId('5c3f4b04c3b498d4a1c6ce22'),
 'name': 'Joe Drumgoole',
 'twitter_handle': '@jdrumgoole'}
>>>

连接到特定的 MongoDB URI

您可以使用MongoDB该类连接到不同的数据库。这是与MongoDB Atlas托管数据库的示例连接。

>>> from mongodbshell import MongoDB
>>> atlas=MongoDB(uri="mongodb+srv://readonly:readonly@demodata-rgl39.mongodb.net/test?retryWrites=true", database="demo", collection="zipcodes")
>>> atlas.find_one()
1    {'_id': '01069',
2     'city': 'PALMER',
3     'loc': [-72.328785, 42.176233],
4     'pop': 9778,
5     'state': 'MA'}

着眼于大量产出

如果您在 python shell 中运行查询,它将返回一个游标并查看游标中的对象,您需要编写一个循环来使用游标或显式调用next()每个游标项。

>>> c=pymongo.MongoClient("mongodb+srv://readonly:readonly@demodata-rgl39.mongodb.net/test?retryWrites=true")
>>> db=c["demo"]
>>> collection=db["zipcodes"]
>>> collection.find()
<pymongo.cursor.Cursor object at 0x105bf1d68>
>>> cursor=collection.find()
>>> next(cursor)
{'_id': '01069', 'city': 'PALMER', 'loc': [-72.328785, 42.176233], 'pop': 9778, 'state': 'MA'}
>>> next(cursor)
{'_id': '01002', 'city': 'CUSHMAN', 'loc': [-72.51565, 42.377017], 'pop': 36963, 'state': 'MA'}
>>>

这是单调乏味的,当对象大到可以滚动到屏幕外时更是如此。这不是问题,mongodbshell因为 MongoDB对象会自动处理漂亮的打印和分页。

>>> atlas.find()
1    {'_id': '01069', 'city': 'PALMER', 'loc': [-72.328785, 42.176233], 'pop': 9778, 'state': 'MA'}
2    {'_id': '01002', 'city': 'CUSHMAN', 'loc': [-72.51565, 42.377017], 'pop': 36963, 'state': 'MA'}
3    {'_id': '01012', 'city': 'CHESTERFIELD', 'loc': [-72.833309, 42.38167], 'pop': 177, 'state': 'MA'}
4    {'_id': '01073', 'city': 'SOUTHAMPTON', 'loc': [-72.719381, 42.224697], 'pop': 4478, 'state': 'MA'}
5    {'_id': '01096', 'city': 'WILLIAMSBURG', 'loc': [-72.777989, 42.408522], 'pop': 2295, 'state': 'MA'}
6    {'_id': '01262', 'city': 'STOCKBRIDGE', 'loc': [-73.322263, 42.30104], 'pop': 2200, 'state': 'MA'}
7    {'_id': '01240', 'city': 'LENOX', 'loc': [-73.271322, 42.364241], 'pop': 5001, 'state': 'MA'}
8    {'_id': '01370', 'city': 'SHELBURNE FALLS', 'loc': [-72.739059, 42.602203], 'pop': 4525, 'state': 'MA'}
9    {'_id': '01340', 'city': 'COLRAIN', 'loc': [-72.726508, 42.67905], 'pop': 2050, 'state': 'MA'}
10   {'_id': '01462', 'city': 'LUNENBURG', 'loc': [-71.726642, 42.58843], 'pop': 9117, 'state': 'MA'}
11   {'_id': '01473', 'city': 'WESTMINSTER', 'loc': [-71.909599, 42.548319], 'pop': 6191, 'state': 'MA'}
12   {'_id': '01510', 'city': 'CLINTON', 'loc': [-71.682847, 42.418147], 'pop': 13269, 'state': 'MA'}
13   {'_id': '01569', 'city': 'UXBRIDGE', 'loc': [-71.632869, 42.074426], 'pop': 10364, 'state': 'MA'}
14   {'_id': '01775', 'city': 'STOW', 'loc': [-71.515019, 42.430785], 'pop': 5328, 'state': 'MA'}
Hit Return to continue (q or quit to exit)

分页将动态调整到屏幕高度。

输出到文件

该类可以通过设置类的属性MongoDB将输出发送到文件。output_fileMongoDB

>>> atlas.output_file="zipcodes.txt"
>>> atlas.find()
Output is also going to 'zipcodes.txt'
1    {'_id': '01069', 'city': 'PALMER', 'loc': [-72.328785, 42.176233], 'pop': 9778, 'state': 'MA'}
2    {'_id': '01002', 'city': 'CUSHMAN', 'loc': [-72.51565, 42.377017], 'pop': 36963, 'state': 'MA'}
3    {'_id': '01012', 'city': 'CHESTERFIELD', 'loc': [-72.833309, 42.38167], 'pop': 177, 'state': 'MA'}
4    {'_id': '01073', 'city': 'SOUTHAMPTON', 'loc': [-72.719381, 42.224697], 'pop': 4478, 'state': 'MA'}
5    {'_id': '01096', 'city': 'WILLIAMSBURG', 'loc': [-72.777989, 42.408522], 'pop': 2295, 'state': 'MA'}
6    {'_id': '01262', 'city': 'STOCKBRIDGE', 'loc': [-73.322263, 42.30104], 'pop': 2200, 'state': 'MA'}
7    {'_id': '01240', 'city': 'LENOX', 'loc': [-73.271322, 42.364241], 'pop': 5001, 'state': 'MA'}
8    {'_id': '01370', 'city': 'SHELBURNE FALLS', 'loc': [-72.739059, 42.602203], 'pop': 4525, 'state': 'MA'}
9    {'_id': '01340', 'city': 'COLRAIN', 'loc': [-72.726508, 42.67905], 'pop': 2050, 'state': 'MA'}
10   {'_id': '01462', 'city': 'LUNENBURG', 'loc': [-71.726642, 42.58843], 'pop': 9117, 'state': 'MA'}
11   {'_id': '01473', 'city': 'WESTMINSTER', 'loc': [-71.909599, 42.548319], 'pop': 6191, 'state': 'MA'}
12   {'_id': '01510', 'city': 'CLINTON', 'loc': [-71.682847, 42.418147], 'pop': 13269, 'state': 'MA'}
13   {'_id': '01569', 'city': 'UXBRIDGE', 'loc': [-71.632869, 42.074426], 'pop': 10364, 'state': 'MA'}
14   {'_id': '01775', 'city': 'STOW', 'loc': [-71.515019, 42.430785], 'pop': 5328, 'state': 'MA'}
>>> print(open('zipcodes.txt').read())
{'_id': '01069', 'city': 'PALMER', 'loc': [-72.328785, 42.176233], 'pop': 9778, 'state': 'MA'}
{'_id': '01002', 'city': 'CUSHMAN', 'loc': [-72.51565, 42.377017], 'pop': 36963, 'state': 'MA'}
{'_id': '01012', 'city': 'CHESTERFIELD', 'loc': [-72.833309, 42.38167], 'pop': 177, 'state': 'MA'}
{'_id': '01073', 'city': 'SOUTHAMPTON', 'loc': [-72.719381, 42.224697], 'pop': 4478, 'state': 'MA'}
{'_id': '01096', 'city': 'WILLIAMSBURG', 'loc': [-72.777989, 42.408522], 'pop': 2295, 'state': 'MA'}
{'_id': '01262', 'city': 'STOCKBRIDGE', 'loc': [-73.322263, 42.30104], 'pop': 2200, 'state': 'MA'}
{'_id': '01240', 'city': 'LENOX', 'loc': [-73.271322, 42.364241], 'pop': 5001, 'state': 'MA'}
{'_id': '01370', 'city': 'SHELBURNE FALLS', 'loc': [-72.739059, 42.602203], 'pop': 4525, 'state': 'MA'}
{'_id': '01340', 'city': 'COLRAIN', 'loc': [-72.726508, 42.67905], 'pop': 2050, 'state': 'MA'}
{'_id': '01462', 'city': 'LUNENBURG', 'loc': [-71.726642, 42.58843], 'pop': 9117, 'state': 'MA'}
{'_id': '01473', 'city': 'WESTMINSTER', 'loc': [-71.909599, 42.548319], 'pop': 6191, 'state': 'MA'}
{'_id': '01510', 'city': 'CLINTON', 'loc': [-71.682847, 42.418147], 'pop': 13269, 'state': 'MA'}
{'_id': '01569', 'city': 'UXBRIDGE', 'loc': [-71.632869, 42.074426], 'pop': 10364, 'state': 'MA'}
{'_id': '01775', 'city': 'STOW', 'loc': [-71.515019, 42.430785], 'pop': 5328, 'state': 'MA'}

输出将继续发送到 ,output_file直到分配了 output_file None或空字符串 ("")。

选项

MongoDB您可以在类对象上设置以下选项。

MongoDB.line_numbers: 布尔。True 以在输出中显示行号,False 以删除它们。

MongoDB.pretty_print: 布尔。True 用于pprint.pprint输出文档。当数据库返回它们时将它们写出是错误的。

MongoDB.paginate: 布尔。真正根据屏幕高度对输出进行分页。False 只是将所有输出直接发送到控制台。

MongoDB.output_file: 力量。定义要写入结果的文件。所有输出都附加到文件中。每行都被刷新,因此内容不会丢失。设置output_file tonNone或 emtpy 字符串 ("") 以停止输出到文件。

项目详情