jlod 2.0.1
Local Document Oriented Database
Requires Python: >=3.6


JLOD stands for JSON Local Document Database, it is a serverless in-process library that implements a self-contained document-oriented database use for storing small application data within an application without  internet connection. The database is like MongoDB, it uses Collection, Document, and JSON objects.  The JLOD  is a version of SQLite for object oriented data, the  JLOD collections can be exported to Remote MongoDB collection as well as remote MongoDB collection can also be imported to the JLOD database.

JLOD is an embedded document-oriented database library. Unlike MongoDB.  JLOD does not have a separate server process. JLOD reads and writes the data to ordinary disk files.  The complete JLOD database along with the collections and the documents are contained in a disk file. The folder is a database in JLOD while the file is a collection, and each object is a document. 


Audience

This library will be quite helpful for all those developers who want to develop serverless web applications using Python. It is meant for programmers with a stronghold on MongoDB concepts.  



Prerequisite

The library assumes that the readers have adequate exposure to MongoDB or any document-oriented database concepts. If you have worked on MongoDB, then it will help you further to grasp the concepts of the library quickly. 


Copyright & Disclaimer

1.	Any user is permitted to reuse, distribute this library 
2.	Any user can contribute to this library




Features of the JLOD Library


•	Self Contained

•	Performance

•	Serverless

•	Installation

•	Create Database

•	Create Collection

•	Add Documents

•	Drop Collection

•	Collection Size

•	Get Documents

•	Query Documents

•	Sorting and Distinct

•	Remove Document

•	Update Document

•	Truncate Collection

•	Export Collection

•	Import Collection




Self Contained

JLOD database stored data in a folder within an application,  once the application is hosted to any server or build by any compiler, the database will remain along with the application. And data can be stored and retrieved without an internet connection.

1. Folder is a database
2. File is a Collection
3. Object is a Document




Performance

Since the JLOD database stored data in a local folder within an application, the performance is extremely awesome. The application will not need to initiate an internet connection to access the stored data.



Serverless

Since the JLOD database stored data in a local folder within an application, there is no need for a server. The database stored where ever the application is hosted.




Structure

JLOD library creates a folder as a Database and creates a file as a collection then adds a JSON array of objects to the files as a group of documents.





Installation

$ pip  install  jlod



 	 from jlod import database

  	 dbi = database.connect('test').instance
  	 client = dbi.collection('users')

	 # Add many  documents to a collection
	 added = client.addMany([
		{"name": "Jhon", "gender": "Male", "age": 24, "city": "New Delhi", "country": "India"},

		{"name": "Zee", "gender": "Female", "age": 20, "city": "Lagos", "country": "Nigeria"}, 

		{"name": "Frank", "gender": "Male", "age": 27, "city": "Toronto", "country": "Canada"}
	 ])
	 if added:
		print("Documents Added")
	 else:
		print("Failed to add Document")

	 # Get all  documents of a collection
	 result = client.documents
	 for doc in result:
	    print(doc)

     # Find document where  "name" is  "Rabie" or "age" is "20"
	 result = client.find({
      "$or": [{"name": {"$eq": "Rabie"}}, {"age": {"$eq": 20}}]
     })

	 # Get specific single from  documents where name is Jhon or age is 24
 	 result = client.get(['name'], {
    	"name": "Jhon",
  		"age": 24
 	 })

	#Get multiple keys
   	 result = client.getOne(['name', 'age'], {
   		 "name": "Jhon",
    	 "age": 24
 	 })




Query Documents
There are two types of logical Operators in JLOAD:
1.	AND
2.	OR
AND => returns document that matches all given conditions
OR=> returns documents that match any  of the given conditions


 	result = client.find({
        "$and": {"name": {"$eq": "Rabie"}}
     })

Using the AND operator. Above  will find  any document that has  key “name”  and value equal to “Rabie” 


	result = client.find({
         "$or": [{"name": {"$eq": "Rabie"}}, {"age": {"$eq": 20}}]
    })


Using the OR operator. Above will get any document that has a key name as “Rabie” or age as “23” in the collection.


	result = client.findOne()


Above will return first one document  from the entire collection.


	result = client.findOne({
          "$and": {"age": {"$gt": 24}}
 	})


Above will return the first one document that has key “age” with value greater than 24.



Sorting and Distinct
1.	Sorting is used in sorting data in certain format Accending or Descending.

see examples below


 	result = client.sort({"age": -1})



Above will sort all documents base on key “age” Deceinding order.




 	result = client.sort({"name": 1},{
  	  "$and": {"age": {"$eq": 20}}
 	})




Above will sort all documents base on key “name” Accending order for all documents that has key “age” with value equal to 20.

1.	Distinct is used to get distinct data from a collection with no duplicate. 


 	result = client.distinct()




Above will return the distinct documents





 	result = client.distinct({
   	 "$and": {"city": {"$eq": "Los Angeles"}}
 	})




Above will return the distinct documents for any document that has key “city” with value “Los Angels”




Remove Document
In JLOD remove is used to remove the entire or specific document from a collection, 

see examples below.


	client.remove()

	client.removeAll()


Output: True
Anyone of the above will remove all documents in the collection


 	result = client.remove({
    	  "$and": {"name": {"$eq": "Jhon"}}
 	})





Output: True
Above will remove any document that has key “name”  with value “Jhon” from the collection




 	result = client.remove({
   	   "$or": {"age": {"$gt": 27}}
 	})




        	Output: True
Above will remove any document that has key “name”  with a value greater than “27”

Update Document
JLOD update is used in updating documents of a specific collection.
see examples below.


	client.update({
   	     "name": "Mary",
    	     "age": 20
 	})


Output: True
Above will update the key “name” to “Mary” and age to “20” for all documents in the collection. And if the key age doesn’t exist in the collection, it will be created


   	result = client.update( {"name": "Jhon", "age": 24},{
    		"$and": {"city": {"$eq": "Los Angeles"}}
 	})





Output: True
Above will update the name to “Jhon” and age to “24” on any document that has a city equal to “Los Angeles”. 

Truncate Document


 	result = client.truncate




Output: True
Above will remove all documents in the collection

Export Collection
Export is used to export the entire or specific document in a JLOD collection to remote MongoDB
see examples below.


 	from jlod import database
 	import pymongo

 	dbi = database.connect('example').instance
 	client = dbi.collection('users')

 	host = pymongo.MongoClient("mongodb://localhost:27017/")
 	mydb = host["example"]
 	mongoClient = mydb["users"]

 	result = client.exportTo(mongoClient)




Output: True
The above will export all the documents in JLOD users collection to MongoDB users collections.



 	result = client.exportTo(mongoClient, {
   	  "$and": {"name": {"$eq": "Zee"}}
 	})




Output: True
Above will export any document that contains a name with value  “Zee” from the JLOD users collection to remote MongoDB users collections.




Import Collection
Import is used for importing entire or specific documents from the remote MongoDB collection 
to JLOD local database collection. See the examples below.


  	from jlod import database
  	import pymongo

  	dbi = database.connect('example').instance
  	client = dbi.collection('users')

 	host = pymongo.MongoClient("mongodb://localhost:27017/")
 	mydb = host["example"]
  	mongoClient = mydb["users"]

  	result = client.importFrom(mongoClient)




Above will import all the documents  from remote MongoDB users collections to JLOD users collection 


 result = client.importFrom(mongoClient, {
    "$and": {"name": {"$eq": "Zee"}}
 })


Above will import  any document that contains key “name” with value “Zee”  from the remote MongoDB users collections  to JLOD users collection







End of the library documentation
If you want to contribute or discover any errors or suggestions on the code or logic, please notify us at contact@jload.org.