Skip to main content

use MongoDB just like json.

Project description

pymongos

简化了一些pymongo的操作,像操作json一样操作你的数据。 因为日常用增删查改最多,所以简化主要是围绕这三个操作来的。

安装

先装依赖

python -m pip install pymongos
python3 -m pip install pymongos

连接数据库

无密码连接

from pymongos import mongodb
table = mongodb(host='127.0.0.1', db="test", table="test")

# 无日志
table = mongodb(host='127.0.0.1', db="test", table="test", level="debug")

密码连接

from pymongos import mongodb
table = mongodb(host='127.0.0.1', db="test", table="test", user="admin", passwd="123456789")

插入数据

不设置_id(MongoDB随机生成)

table += {"字段1": "数据1", "字段2": "数据1"}

or

table + {"字段1": "数据1", "字段2": "数据1"}

因为分配的ID是随机的,不建议使用

设置_id

table["设置_id"] = {"字段1": "数据3", "字段2": "数据4"}
table["test"] = {"爱好": "看书", "书名": "鬼吹灯"}

图片传不上来了

删除数据

仅支持指定key方式

del table["设置_id"]

修改

table["test"] = {"书名": "鬼吹灯和三体"}
table["test"] = {"书名": "《鬼吹灯》《三体》", "爱好": "看书&游泳"}
table["test"] = {"name" : "e4ting"}

取数据

表长度

len(table)

_id是否存在

if "test" in table:
    print("yes")

获取所有_id

table.keys()

根据_id取

data = table["test"]
print(data)

取整个表数据

数据少的时候才可以这么豪横的用
table[:]
# 或
table.get()

遍历表

表太大的时候,迭代取
for _ in table:
    print(_)

过滤

table[::{"name":"e4ting"}]
# 或
table.get(name="e4ting")
# 或 字段名有中文时
table.get(**{"字段1":"数据1"})

分段

table[:10]
# 或
table[1:10]
# 或
table.get(limit=10, offset=1)

排序

# 按 _id 倒序
table[::-1]

# 按 _id 倒序,分段
table[0:10:-1]

# 按 name 正序
table[::"name"]

# 按 name 正序,分段
table[0:10:"name"]

# 按 name 倒序
table[::"!name"]

# 按 name 倒序,分段取
table[0:10:"!name"]

搜索

table.search(name="4ting")

搜索分段

table.search(limit=10, offset=0, **{"字段1" : "数据"})

其他操作

关闭连接

del table

获取所有库名

table.dbs

获取该库下所有collection

table.tables

获取所有字段名

table.colums

关闭日志

table.none_log()

开启日志

table.en_log()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pymongos-1.2.2.tar.gz (5.4 kB view details)

Uploaded Source

File details

Details for the file pymongos-1.2.2.tar.gz.

File metadata

  • Download URL: pymongos-1.2.2.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.6.10

File hashes

Hashes for pymongos-1.2.2.tar.gz
Algorithm Hash digest
SHA256 94504abd3c29052af7170134af5eff71f77f89688e0ec9f59a4ddc5df5f8a41d
MD5 9ad3f76981efba1ec8ed88616e357aba
BLAKE2b-256 b20b78d81f07208a6c77319a39475d3a70b99a16063387b12265c3169c8dd7bc

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page