TinyMongo is a wrapper for MongoDB on top of TinyDB.
This is awesome for testing, where you need a local document orientated database which is backed by a flat file. It feels just like using MongoDB, except that its local, lightweight and using TinyDB in the backend.
Installing Dependencies:
1
$ pip install tinymongo
Usage Examples:
Initialize tinymongo and create the database and collection:
1
2
3
4
>>> from tinymongo import TinyMongoClient
>>> connection = TinyMongoClient ( 'foo' )
>>> db_init = connection . mydb
>>> db = db_init . users
Insert a Document, catch the document id and search for that document:
1
2
3
4
>>> record_id = db . insert_one ({ 'username' : 'ruanb' , 'name' : 'ruan' , 'age' : 31 , 'gender' : 'male' , 'location' : 'south africa' }) . inserted_id
>>> user_info = db . find_one ({ "_id" : record_id })
>>> print ( user_info )
{ u'username' : u'ruanb' , u'name' : u'ruan' , u'gender' : u'male' , u'age' : 31 , u'_id' : u'8d2ce01140ec11e888110242ac110004' , u'location' : u'south africa' }
Update a document: Update the age attribute from 31 to 32
1
2
3
>>> db . users . update_one ({ '_id' : '8d2ce01140ec11e888110242ac110004' }, { '$set' : { 'age' : 32 }})
>>> print ( user_info )
{ u'username' : u'ruanb' , u'name' : u'ruan' , u'gender' : u'male' , u'age' : 32 , u'_id' : u'8d2ce01140ec11e888110242ac110004' , u'location' : u'south africa' }
Insert some more data:
1
2
>>> record_id = db . insert_one ({ 'username' : 'stefanb' , 'name' : 'stefan' , 'age' : 30 , 'gender' : 'male' , 'location' : 'south africa' }) . inserted_id
>>> record_id = db . insert_one ({ 'username' : 'alexa' , 'name' : 'alex' , 'age' : 34 , 'gender' : 'male' , 'location' : 'south africa' }) . inserted_id
Find all the users, sorted by descending age, oldest to youngest:
1
2
3
4
5
6
7
>>> response = db . users . find ( sort = [( 'age' , - 1 )])
>>> for doc in response :
... print ( doc )
...
{ u'username' : u'alexa' , u'name' : u'alex' , u'gender' : u'male' , u'age' : 34 , u'_id' : u'66b1cc3d40ee11e892980242ac110004' , u'location' : u'south africa' }
{ u'username' : u'ruanb' , u'name' : u'ruan' , u'gender' : u'male' , u'age' : 32 , u'_id' : u'8d2ce01140ec11e888110242ac110004' , u'location' : u'south africa' }
{ u'username' : u'stefanb' , u'name' : u'stefan' , u'gender' : u'male' , u'age' : 30 , u'_id' : u'fbe9da8540ed11e88c5e0242ac110004' , u'location' : u'south africa' }
Find the number of documents in the collection:
1
2
>>> db . users . find () . count ()
3
Resources: