Skip to main content

Minimalistic pymongo object wrapper

Document

Minimal document model:

>>> from mongo import Document, Index
>>> class Doc(Document):
...     __database__ = 'mongo_test'  # database name

All allowed settings:

>>> class Doc(Document):
...     __connection__ = {'host': 'localhost', 'port': 27017}
...     #__auth__ = ('username', 'password')
...     __database__ = 'mongo_test' # database name
...     __collection__ = 'test'     # collection name
...     __safe__ = True             # enable safe insert mode
...
...     __indexes__ = [
...         Index('number', unique=True, sparse=True),
...     ]

Clear collection

>>> t = Doc.remove()
>>> Doc.count()
0

Create document

>>> doc = Doc(foo='bar')
>>> doc.number = 10
>>> doc.save()
>>> '_id' in doc
True

Fetch

Get by id:

>>> doc1 = Doc.get_by_id(doc.id)
>>> doc1 == doc
True

Find one:

>>> doc2 = Doc.find_one({'number': 10})
>>> doc2 == doc
True

Find:

>>> doc3 = Doc.find({'number': 10}).limit(1)[0]
>>> doc3 == doc
True

Get or create:

>>> doc4, new = Doc.get_or_create({'number': 10}, defaults={'foo': 'bar'})
>>> new
False
>>> doc4 == doc
True

Update

Update filtered documents:

>>> t = Doc.update({'number': 10}, {'$set': {'text': 'foo'}})
>>> doc = Doc.find_one({'number': 10})
>>> doc.text
u'foo'

Save only some fields:

>>> doc.text = u'bar'
>>> doc.number = 11
>>> doc.save_fields('text')
>>> doc = Doc.find_one({'number': 10})
>>> doc.text, doc.number
(u'bar', 10)

Update only some fields:

>>> doc.atomic_update({'$inc': {'number': 2}})
>>> doc = Doc.find_one({'text': 'bar'})
>>> doc.number
12

Counting

>>> Doc().save()
>>> Doc.count()
2
>>> Doc.find({'number': 12}).count()
1

Delete

Remove single document:

>>> t = doc.delete()
>>> Doc.find_one({'number': 12})

Remove some documents:

>>> t = Doc.remove({'number': 10})

Remove all documents:

>>> t = Doc.remove()

Release files for mongo 0.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mongo 0.0.2
File Size Uploaded
mongo-0.0.2.tar.gz 4.5 kB Details

Release files / mongo-0.0.2.tar.gz

Download URL mongo-0.0.2.tar.gz
Size 4.5 kB
Tags Source
SHA-256 checksum
How to use checksums
a78be93df474d5dd21048e80af7962518da7ff76c01fe32e38714386377f77c8
BLAKE2b-256 checksum
How to use checksums
9d87482990fb6dcc416b0545b97a1ea208db6c75768e22041c1d8006ab350271
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

0.2.0

1 release file

0.1.0

1 release file

0.0.4

1 release file

0.0.3

1 release file

This release

0.0.2 This release

1 release file

0.0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page