MongoEngine & serverStatus

I am switching my python script using pymongo to use mongoengine. I used to have this call:

client_count = db.command("serverStatus")["connections"]['current'] - 1

but I want to get the number of current connections using mongoengine wrapper. I looked at the server-status and mongoengine docs and couldn't find an equivalent call to pull server variables.

I want to switch to mongoengine because we are using its ODM features and we would like to reduce redundancy.

Any pointers to this? What is the correct syntax, if at all? Thank.

In the meantime, we're just going to use pymongo to get the correct data, using mongoengine for everything else.

+3


source to share


2 answers


There is no Mongoengine equivalent, so you have to call it manually. When we terminate pymongo, you can make the call by getting the database from the connection register. The easiest way to do this is to use a document class like:



db = Document._get_db()
client_count = db.command("serverStatus")["connections"]['current'] - 1

      

+6


source


Caveat: I don't know any of these APIs well.

However, if you look at the dependencies of mongoengine, you can see that it depends on pymongo as it is actually implemented from the pymongo point of view.



Now take a look at the mongoengines implementation of your connection logic: https://github.com/MongoEngine/mongoengine/blob/master/mongoengine/connection.py#L113 : The connection objects it returns are actually pymongo connections. So, in principle, you should be able to make pymongo calls on the returned connection. So you could keep using the above call. Of course, you are breaking mongoengine's encapsulation at this point, as mongoengine may change its internal implementation type for connections.

However, looking at their documentation, I don't see any other way to access this aspect of the database, so this is perhaps the easiest way to go forward.

+1


source







All Articles