database package

database.database_server module

Server for communicating with MongoDB databases

class database.database_server.DatabaseServer

Bases: LabradServer

Server for communicating with MongoDB databases.

Currently includes methods for inserting, modifying, and deleting data, and the basic query method find_one().

Uses bson.json_util for serializing and deserializing BSON data.

static DeleteResultToDict(res)

Converts a pymongo.results.DeleteResult to a dictionary.

Parameters:

res (pymongo.results.DeleteResult) – The result to convert to a dictionary

Returns:

A dictionary containing the result’s fields

Return type:

dict

static InsertManyResultToDict(res)

Converts a pymongo.results.InsertManyResult to a dictionary.

Parameters:

res (pymongo.results.InsertManyResult) – The result to convert to a dictionary

Returns:

A dictionary containing the result’s fields

Return type:

dict

static InsertOneResultToDict(res)

Converts a pymongo.results.InsertOneResult to a dictionary.

Parameters:

res (pymongo.results.InsertOneResult) – The result to convert to a dictionary

Returns:

A dictionary containing the result’s fields

Return type:

dict

static UpdateResultToDict(res)

Converts a pymongo.results.UpdateResult to a dictionary.

Parameters:

res (pymongo.results.UpdateResult) – The result to convert to a dictionary

Returns:

A dictionary containing the result’s fields

Return type:

dict

close(self, c)

Close the client’s context’s connection to the database.

Parameters:

c – LabRAD context

connect(self, c, address=None, port=None, user=None, password=None, database=None, collection=None)

Connect to a MongoDB database. Connections are maintained per LabRAD context.

Parameters:
  • c – LabRAD context

  • address (str, optional) – The address to connect to. Defaults to None, in which case the address is loaded from mongodb.json if the file exists.

  • port (str, optional) – The port to connect to. Defaults to None, in which case the port is loaded from mongodb.json if the file exists. If the port is not specified in the file, the default port is 27017.

  • user (str, optional) – The user to connect as. Defaults to None, in which case the user is loaded from mongodb.json if the file exists.

  • password (str, optional) – The user’s password. Defaults to None, in which case the password is loaded from mongodb.json if the file exists.

  • database (str, optional) – The database to select. Defaults to None, in which case :meth:set_database must be run to set the client’s context’s database.

  • collection (str, optional) – The database’s collection to select. Defaults to None, in which case :meth:set_collection must be run to set the client’s context’s database’s collection.

  • timeout (int, optional) – The timeout in ms for connecting to the database (connectTimeoutMS in pymongo.mongo_client.MongoClient). Defaults to 2000.

Returns:

A BSON-dumped string of the result of pymongo.mongo_client.MongoClient.server_info() if the connection was successful and the string {} otherwise.

Return type:

str

delete_many(self, c, db_filter)

Delete one or more documents matching db_filter.

See the PyMongo documentation pymongo.collection.Collection.delete_many() for information on how filter (called filter in the documentation) works.

Parameters:
  • c – LabRAD context

  • db_filter (str) – BSON-dumped string of the filter

Returns:

A BSON-dumped string of the pymongo.results.DeleteResult

Return type:

str

delete_one(self, c, db_filter)

Delete a single document matching db_filter.

See pymongo.collection.Collection.delete_one() for information on how db_filter (called filter in the documentation) works.

Parameters:
  • c – LabRAD context

  • db_filter (str) – BSON-dumped string of the filter

Returns:

A BSON-dumped string of the pymongo.results.DeleteResult

Return type:

str

expireContext(self, c)

Called when the context expires, typically by the client disconnecting.

Calls close().

Parameters:

c – LabRAD context

find_one(self, c, db_filter, projection=None)

Get a single document matching db_filter.

See pymongo.collection.Collection.find_one() for information on how db_filter (called filter in the documentation) works.

Parameters:
  • c – LabRAD context

  • db_filter (str) – BSON-dumped string of the filter

  • projection ([str]) – a list of field names that should be returned in the result set. Defaults to None, in which case all fields are returned.

Returns:

A BSON-dumped string of the document or {} if no document is found

Return type:

str

insert_many(self, c, documents, ordered=True)

Inserts a list of documents.

See pymongo.collection.Collection.insert_many().

Parameters:
  • c – LabRAD context

  • documents (list of str) – list of BSON-dumped strings of the documents to insert

  • ordered (bool, optional) – If True (the default) documents will be inserted on the server serially, in the order provided. If an error occurs all remaining inserts are aborted. If False, documents will be inserted on the server in arbitrary order, possibly in parallel, and all document inserts will be attempted.

Returns:

A BSON-dumped string of the pymongo.results.InsertManyResult if the operation was successful and the string {} otherwise.

Return type:

str

insert_one(self, c, document)

Insert a single document.

See pymongo.collection.Collection.insert_one().

Parameters:
  • c – LabRAD context

  • document (str) – BSON-dumped string of the document

Returns:

A BSON-dumped string of the pymongo.results.InsertOneResult if the operation was successful and the string {} otherwise.

Return type:

str

name = 'database'
replace_one(self, c, db_filter, replacement, upsert=False)

Replace a single document matching db_filter with replacement.

See pymongo.collection.Collection.replace_one() for information on how db_filter (called filter in the documentation) and replacement work.

Parameters:
  • c – LabRAD context

  • db_filter (str) – BSON-dumped string of the filter

  • replacement (str) – BSON-dumped string of update

  • upsert (bool, optional) – Whether to create a document if none exists. Defaults to False.

Returns:

A BSON-dumped string of the pymongo.results.UpdateResult

Return type:

str

set_collection(self, c, collection)

Sets the collection associated with the client’s context. :meth:set_database must already have been called.

Parameters:
  • c – LabRAD context

  • collection (str) – The name of the collection to use

Returns:

True if the collection exists in the current database, False otherwise

Return type:

bool

set_database(self, c, database)

Sets the database associated with the client’s context.

Parameters:
  • c – LabRAD context

  • database (str) – The name of the database to use

Returns:

True if the database exists, False otherwise

Return type:

bool

update_many(self, c, db_filter, update, upsert=False)

Update one or more documents matching db_filter with update.

See :py:meth:pymongo.collection.Collection.update_many for information on how db_filter (called filter in the documentation) and update work.

Parameters:
  • c – LabRAD context

  • db_filter (str) – BSON-dumped string of the filter

  • update (str) – BSON-dumped string of update

  • upsert (bool, optional) – Whether to create a document if none exists. Defaults to False.

Returns:

A BSON-dumped string of the pymongo.results.UpdateResult

Return type:

str

update_one(self, c, db_filter, update, upsert=True)

Update a single document matching db_filter with update.

See pymongo.collection.Collection.update_one() for information on how db_filter (called filter in the documentation) and update work.

Parameters:
  • c – LabRAD context

  • db_filter (str) – BSON-dumped string of the filter

  • update (str) – BSON-dumped string of update

  • upsert (bool, optional) – Whether to create a document if none exists. Defaults to True.

Returns:

A BSON-dumped string of the pymongo.results.UpdateResult

Return type:

str