database package
database.database_server module
Server for communicating with MongoDB databases
- class database.database_server.DatabaseServer
Bases:
LabradServerServer for communicating with MongoDB databases.
Currently includes methods for inserting, modifying, and deleting data, and the basic query method
find_one().Uses
bson.json_utilfor serializing and deserializing BSON data.- static DeleteResultToDict(res)
Converts a
pymongo.results.DeleteResultto a dictionary.- Parameters:
res (
pymongo.results.DeleteResult) – The result to convert to a dictionary- Returns:
A dictionary containing the result’s fields
- Return type:
- static InsertManyResultToDict(res)
Converts a
pymongo.results.InsertManyResultto a dictionary.- Parameters:
res (
pymongo.results.InsertManyResult) – The result to convert to a dictionary- Returns:
A dictionary containing the result’s fields
- Return type:
- static InsertOneResultToDict(res)
Converts a
pymongo.results.InsertOneResultto a dictionary.- Parameters:
res (
pymongo.results.InsertOneResult) – The result to convert to a dictionary- Returns:
A dictionary containing the result’s fields
- Return type:
- static UpdateResultToDict(res)
Converts a
pymongo.results.UpdateResultto a dictionary.- Parameters:
res (
pymongo.results.UpdateResult) – The result to convert to a dictionary- Returns:
A dictionary containing the result’s fields
- Return type:
- 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 frommongodb.jsonif the file exists.port (str, optional) – The port to connect to. Defaults to
None, in which case the port is loaded frommongodb.jsonif the file exists. If the port is not specified in the file, the default port is27017.user (str, optional) – The user to connect as. Defaults to
None, in which case the user is loaded frommongodb.jsonif the file exists.password (str, optional) – The user’s password. Defaults to
None, in which case the password is loaded frommongodb.jsonif 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 (
connectTimeoutMSinpymongo.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:
- 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 howfilter(calledfilterin 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:
- delete_one(self, c, db_filter)
Delete a single document matching
db_filter.See
pymongo.collection.Collection.delete_one()for information on howdb_filter(calledfilterin 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:
- 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 howdb_filter(calledfilterin the documentation) works.- Parameters:
- Returns:
A BSON-dumped string of the document or
{}if no document is found- Return type:
- 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. IfFalse, 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.InsertManyResultif the operation was successful and the string{}otherwise.- Return type:
- 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.InsertOneResultif the operation was successful and the string{}otherwise.- Return type:
- name = 'database'
- replace_one(self, c, db_filter, replacement, upsert=False)
Replace a single document matching
db_filterwithreplacement.See
pymongo.collection.Collection.replace_one()for information on howdb_filter(calledfilterin the documentation) andreplacementwork.- Parameters:
- Returns:
A BSON-dumped string of the
pymongo.results.UpdateResult- Return type:
- set_collection(self, c, collection)
Sets the collection associated with the client’s context. :meth:set_database must already have been called.
- set_database(self, c, database)
Sets the database associated with the client’s context.
- update_many(self, c, db_filter, update, upsert=False)
Update one or more documents matching
db_filterwithupdate.See :py:meth:pymongo.collection.Collection.update_many for information on how
db_filter(calledfilterin the documentation) andupdatework.- Parameters:
- Returns:
A BSON-dumped string of the
pymongo.results.UpdateResult- Return type:
- update_one(self, c, db_filter, update, upsert=True)
Update a single document matching
db_filterwithupdate.See
pymongo.collection.Collection.update_one()for information on howdb_filter(calledfilterin the documentation) andupdatework.- Parameters:
- Returns:
A BSON-dumped string of the
pymongo.results.UpdateResult- Return type: