A small Python MongoDB Document-Based access engine
Project description
PMongo
A small Python MongoDB Document-Based access engine.
Install
pip install pmongo
Useage
# base use
# import
from pmongo.document import Document
from pmongo.utils import get_mongo_db
db = get_mongo_db(dbname='test')
# define document class
class Data(Document):
db = db
class Meta:
ordering = ['age']
# new a instance
d1 = Data()
# set data and save
d1['grade'] = 2
d1['name'] = 'Tom'
d1['age'] = 8
d1.save()
d2 = Data()
d2['grade'] = 2
d2['name'] = 'Lucy'
d2['age'] = 6
d2.save()
d3 = Data()
d3['grade'] = 3
d3['name'] = 'Jack'
d3['age'] = 7
d3.save()
# display document ObjectId
print('d1.id:', d1.id)
# query
print('grade=2:', Data.objects.find(grade=2).all())
# query count
print('count of grade=2:', Data.objects.find(grade=2).count())
# default order by
print('default order by', Data.objects.find().all())
print('default order by -age', Data.objects.find().order_by('-age').all())
# change
d2['grade'] = 1
d2.save()
print('count of grade=2:', Data.objects.find(grade=2).count())
# delete document
Data.objects.find(grade=2).delete()
print('-------')
# django like query
from pmongo.query import QueryManger
class Data2(Document):
db = db
objects = QueryManger()
print(Data2.objects.create(age=20, name='Tom'))
Data2(age=10, name='Jone').save()
Data2(age=15, name='Jack').save()
print('age>=10:', Data2.objects.find(age__gte=10).count())
print('age>11:', Data2.objects.find(age__gt=11).count())
print('between 10~21', Data2.objects.find(age__between=(10, 21)).count())
# delete age field
d1.unset(['age'])
print(d1)
# update data
print('update', Data2.objects.find(age__between=(10, 21)).update(age=25))
print('between 10~21', Data2.objects.find(age__between=(10, 21)).count())
print('age=25', Data2.objects.find(age=25).count())
Data(name='Robin', books=[{'bid': 1, 'name': 'Python Cookbook'}, {'bid': 2, 'name': 'Java 23 Days'}, {'bid': 3, 'name': 'Android Kit'}]).save()
Data(name='Tom', books=[{'bid': 10, 'name': 'DDL'}]).save()
print('book of [1]:', Data.objects.find({'books.bid': 1}).count())
print('book of [2, 10]:', Data.objects.find({'books.bid': {'$in': [2, 10]}}).count())
print(Data.objects.find(name='Tom').values(name=0, _id=0).all())
d1 = Data.objects.find().first()
d2 = Data.objects.find().first()
print('--------')
print(id(d1), d1)
print(id(d2), d2)
print(d1 == d2)
print(set([d1, d2]))
print(Data.objects.find().delete())
print(Data2.objects.find().delete())
Output:
d1.id: 5bf7ddc8f56f4bc0d639a572
grade=2: [Data[{'_id': ObjectId('5bf7ddc8f56f4bc0d639a573'), 'grade': 2, 'name': 'Lucy', 'age': 6}], Data[{'_id': ObjectId('5bf7ddc8f56f4bc0d639a572'), 'grade': 2, 'name': 'Tom', 'age': 8}]]
count of grade=2: 2
default order by [Data[{'_id': ObjectId('5bf7ddc8f56f4bc0d639a573'), 'grade': 2, 'name': 'Lucy', 'age': 6}], Data[{'_id': ObjectId('5bf7ddc8f56f4bc0d639a574'), 'grade': 3, 'name': 'Jack', 'age': 7}], Data[{'_id': ObjectId('5bf7ddc8f56f4bc0d639a572'), 'grade': 2, 'name': 'Tom', 'age': 8}]]
default order by -age [Data[{'_id': ObjectId('5bf7ddc8f56f4bc0d639a572'), 'grade': 2, 'name': 'Tom', 'age': 8}], Data[{'_id': ObjectId('5bf7ddc8f56f4bc0d639a574'), 'grade': 3, 'name': 'Jack', 'age': 7}], Data[{'_id': ObjectId('5bf7ddc8f56f4bc0d639a573'), 'grade': 2, 'name': 'Lucy', 'age': 6}]]
count of grade=2: 1
-------
Data2[{'age': 20, 'name': 'Tom', '_id': ObjectId('5bf7ddc8f56f4bc0d639a575')}]
age>=10: 3
age>11: 2
between 10~21 3
Data[{'grade': 2, 'name': 'Tom', '_id': ObjectId('5bf7ddc8f56f4bc0d639a572')}]
update 3
between 10~21 0
age=25 3
book of [1]: 1
book of [2, 10]: 2
[Data[{'books': [{'bid': 10, 'name': 'DDL'}]}]]
--------
4475195856 Data[{'_id': ObjectId('5bf7ddc8f56f4bc0d639a573'), 'grade': 1, 'name': 'Lucy', 'age': 6}]
4475196976 Data[{'_id': ObjectId('5bf7ddc8f56f4bc0d639a573'), 'grade': 1, 'name': 'Lucy', 'age': 6}]
False
{Data[{'_id': ObjectId('5bf7ddc8f56f4bc0d639a573'), 'grade': 1, 'name': 'Lucy', 'age': 6}], Data[{'_id': ObjectId('5bf7ddc8f56f4bc0d639a573'), 'grade': 1, 'name': 'Lucy', 'age': 6}]}
4
3
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
pysmongo-1.0.0.tar.gz
(24.1 kB
view details)
Built Distribution
pysmongo-1.0.0-py3-none-any.whl
(25.6 kB
view details)
File details
Details for the file pysmongo-1.0.0.tar.gz
.
File metadata
- Download URL: pysmongo-1.0.0.tar.gz
- Upload date:
- Size: 24.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.9.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4b60d6b9a4696c2e97d5bc51e1b0ce61b1d9ff9bae072988f37e56132cee9b2b |
|
MD5 | 778e60511ee81a67d423854d0d46de4a |
|
BLAKE2b-256 | 107d73028b4be46bf35edb31c7579bbccceccbc22f5fffe8b2b9a7cbbc4ac554 |
File details
Details for the file pysmongo-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: pysmongo-1.0.0-py3-none-any.whl
- Upload date:
- Size: 25.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.9.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e983961e36f24c2197c8ff361f5616fb21df1220dbf01ba37db21a8f830b0752 |
|
MD5 | 767a61bcd625742a856a073a4dcdc651 |
|
BLAKE2b-256 | bb76fe37f53295ea256f8ec7ab5411ff201d8bb24097e44e231e47668411c61b |