Package json_to_relation :: Module mongodb :: Class MongoDB
[hide private]
[frames] | no frames]

Class MongoDB

source code

object --+
         |
        MongoDB

Very simple Python interface to MongoDB. Based on pymongo, this class provides methods to get and set default databases and collections, to insert documents, query collections, and clear all documents in a collection.

The query() method encapsulates MangoDB native methods find() and find_one(). The query() method makes it very convenient to to request only particular sets of fields (columns in relational terms). Example:

   myMongoDb.query({'lname' : 'Doe'}, ('fname', 'lname', 'age'))
Instance Methods [hide private]
 
__init__(self, host='localhost', ssl_keyfile=None, dbName='test', collection='test_collection', port=27017, user=None, pwd='')
Create a connection to the MongoDB demon on the given host/port.
source code
 
setDB(self, dbName)
Establish a default database within MongoDB for subsequent calls to other methods of this class.
source code
String
getDBName(self)
Obtain the name of the MongoDB database that is currently the default for calls to methods of this class.
source code
 
setCollection(self, collName)
Establish a default MongoDB collection for subsequent calls to other methods of this class.
source code
String
getCollectionName(self)
Obtain the name of the MongoDB collection that is currently the default for calls to methods of this class.
source code
{generator<ResultDict>}
query(self, mongoQuery, colNameTuple=(), limit=0, db=None, collection=None)
Method for querying the database.
source code
{int | None}
resultCount(self, queryDict)
Return number of results in the given query.
source code
 
clearCollection(self, db=None, collection=None)
Remove all documents from a collection.
source code
 
dropCollection(self, db=None, collection=None)
Remove a collection from the database.
source code
 
insert(self, doc_or_docs, db=None, collection=None)
Insert the given dictionary into a MongoDB collection.
source code
 
close(self)
Release all resources.
source code
 
get_db(self)
Obtain current default MongoDB database object
source code
 
get_collection(self)
Obtain current default MongoDB database object
source code

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Class Variables [hide private]
  queryCursors = {}
Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, host='localhost', ssl_keyfile=None, dbName='test', collection='test_collection', port=27017, user=None, pwd='')
(Constructor)

source code 

Create a connection to the MongoDB demon on the given host/port.

Parameters:
  • host (String) - host name where MongoDB demon is running. Can be IP address as string.
  • port (int) - MongoDB demon's port
Overrides: object.__init__

setDB(self, dbName)

source code 

Establish a default database within MongoDB for subsequent calls to other methods of this class.

Parameters:
  • dbName (String) - MongoDB database name

setCollection(self, collName)

source code 

Establish a default MongoDB collection for subsequent calls to other methods of this class.

Parameters:
  • collName (String) - MongoDB collection name

query(self, mongoQuery, colNameTuple=(), limit=0, db=None, collection=None)

source code 

Method for querying the database. The mongoQuery parameter is a dictionary conforming to the MongoDB query conventions. This query is passed through to the underlying MongoDB.

The colNameTuple contains a list of field (a.k.a. relational column) names. Result documents will contain only those fields. In contrast to MangoDB convention, the _id field is not automatically returned in the result dictionaries. This field is only included if the caller lists it in colNameTuple. If colNameTuple is an empty tuple, the entirety of each document is returned for each query result.

The limit parameter determines how many documents will be returned. A value of zero returns the entire result set. A value of 1 makes the method's behavior analogous to MangoDB's native find_one() method.

The db and collection keyword arguments allow callers to address a MongoDB database and/or collection that are not the default. After the method returns, the default database and collection values will still be untouched.

Parameters:
  • mongoQuery (Dict<String,<any>>) - MangoDB query
  • colNameTuple ((String)) - a possibly empty tuple of field/column names to retrieve for each result document
  • limit (int) - maximum number of documents to return
  • db (String) - name of MongoDB database other than the current default
  • collection (String) - name of MongoDB collection other than the current default
Returns: {generator<ResultDict>}

resultCount(self, queryDict)

source code 

Return number of results in the given query. Only works when query has previously been issued via the query() method AND at least one result has been extracted. That's because the first call to query() only returns a generator. This isn't good.

Parameters:
  • queryDict (Dict<String,<any>>) - Same query that was provided to the query() method
Returns: {int | None}
number of results, taking into account limit provided to query(). None if no result has been pulled from query()

clearCollection(self, db=None, collection=None)

source code 

Remove all documents from a collection. The affected database/collection are the current defaults, if database/collection are None, else the specified database/collection is affected.

Parameters:
  • db (String) - Name of MongoDB database, or None
  • collection (String) - Name of MongoDB collection, or None

dropCollection(self, db=None, collection=None)

source code 

Remove a collection from the database. The affected database/collection are the current defaults, if database/collection are None, else the specified database/collection is affected.

Parameters:
  • db (String) - Name of MongoDB database, or None
  • collection (String) - Name of MongoDB collection, or None

insert(self, doc_or_docs, db=None, collection=None)

source code 

Insert the given dictionary into a MongoDB collection.

Parameters:
  • doc_or_docs (Dict<String,<any>>) - Dictionary whose entries are the documents
  • db (String) - Name of MongoDB database, or None
  • collection (String) - Name of MongoDB collection, or None