nordlys.core.storage.mongo module

Mongo

Tools for working with MongoDB.

Authors:Krisztian Balog, Faegheh Hasibi
class nordlys.core.storage.mongo.Mongo(host, db, collection)[source]

Bases: object

Manages the MongoDB connection and operations.

ID_FIELD = '_id'
add(doc_id, contents)[source]

Adds a document or replaces the contents of an entire document.

append_dict(doc_id, field, dictkey, value)[source]

Appends the value to a given field that stores a dict. If the dictkey is already in use, the value stored there will be overwritten.

Parameters:
  • doc_id – document id
  • field – field
  • dictkey – key in the dictionary
  • value – value to be increased by
append_list(doc_id, field, value)[source]

Appends the value to a given field that stores a list. If the field does not exist yet, it will be created. The value should be a list.

Parameters:
  • doc_id – document id
  • field – field
  • value – list, a value to be appended to the current list
append_set(doc_id, field, value)[source]

Adds a list of values to a set. If the field does not exist yet, it will be created. The value should be a list.

Parameters:
  • doc_id – document id
  • field – field
  • value – list, a value to be appended to the current list
drop()[source]

Deletes the contents of the given collection (including indices).

find_all(no_timeout=False)[source]

Returns a Cursor instance that allows us to iterate over all documents.

find_by_id(doc_id)[source]

Returns unescaped document content for a given document id.

get_num_docs()[source]

Returns total number of documents in the mongo collection.

inc(doc_id, field, value)[source]

Increments the value of a specified field.

inc_in_dict(doc_id, field, dictkey, value=1)[source]

Increments a value that is inside a dict.

Parameters:
  • doc_id – document id
  • field – field
  • dictkey – key in the dictionary
  • value – value to be increased by
static print_doc(doc)[source]
set(doc_id, field, value)[source]

Sets the value of a given document field (overwrites previously stored content).

static unescape(s)[source]

Unescapes string.

static unescape_doc(mdoc)[source]

Unescapes document content.

nordlys.core.storage.mongo.main()[source]