I’m working on an API which requires the primary node’s name from an mongodb replia set. Is there a way to get the name and ip of primary/master node in a replicaset using the pymongo API? Or generally any way i could get that info from a python program? Answer Use the hello command. It will return hello.primary A string
Tag: pymongo
how to find $avg and $sum for fields which contain NaN value in mongodb?
I can find and limit columns which contain NaN value before using $group clause in mongodb when I use mongo cli or JavaScript. However, when I use python and its major library “pymongo” it seems not to be able to do the same. As following code NaN is not part of python syntax. Whereas it is easy and straight forward
How to create MongoDB Time Series Collection using pymongo
The documentation shows how to do it with mongosh, but how do you create Time Series Collection using pymongo from within a python script? Answer You can try this: General Reference: Time Series Collections
Sort an array by occurances mongodb
Is it possible to sort an array by occurrences? For Example, given return I am able to get it most of the way there but I cannot figure out how to join them all back together in an array. I have been using an aggregation pipeline Starting with $match to find the site I want Then $unwind on with path:
my leveling bot(discord) seems to gives a error Command raised an exception: AttributeError: ‘tuple’ object has no attribute ‘find_one’
My code: Connection to the db code: Error: Command raised an exception: AttributeError: ‘tuple’ object has no attribute ‘find_one’ Answer You should change the connection to the db code to:
Mongo append to standardized string field based on mapping of multiple fields (including nested)
I have a large collection with fields like: I also have a Python dictionary mapping field values like: I’m trying to (in PyMongo) update my MongoDB collection so that a string field of mapped characters is accumulated, from both the top-level class field and the nested nested.classification fields. In the above, this would produce the following updates: How can I
How to dynamically code collection name inside a function?
I am trying to code a Python function to insert my CSV file to MongoDB database using MongoClient() but I don’t know how to dynamically code the collection name. Here is my code: If I write my code like above, the collection name will be “file_name” instead of the parameter file_name. Anyone knows how to fix this? Thank you! Answer
How to convert a whole column from string type to date type in mongodb with pymongo
My data consist of 1million rows. A sample look like this: The thing is that date2 and date4 are in the form that i want but they are string and i want to convert them to date. The code i have used look like this: Do i need to convert them before inserting or after? Does anyone know how i
pymongo converts . variables into a dict
I am inserting the data to mongoDB collection through pymongo. I have logged all the information and data which is being sent to update_one statement. Data which is logged just before update_one statement : But when it got inserted into “test” then it got appended like this : Using this to update the document: So here you’ll see parsed data
pymongo update all documents in collection for field with it’s substring value
Desired output in other words substring [:3] How can I achieve this with pymongo Answer You can use updatemany with an aggregation : https://docs.mongodb.com/manual/tutorial/update-documents-with-aggregation-pipeline/