My Coding >
Programming language >
Python >
Python and SQL >
Python and MongDB >
Pymongo: browse databases
Pymongo: browse databasesStarting to work with new collection in a new database it is very important to see what is stored in this database, check the name of collections etc. Here I will show you how to do this first step of analysis of the existing collection. I will describe this code step by step, but you need to run all parts. List of DatabasesWe will use pymongo for working with MongoDB and also pprint for nice output of JSON data. First of all it is necessary to connect to MongoDB with MongoClient and then make a list of all databases with list_database_names() method
It is possible to see that we have 4 databases, and intro_db looks interesting for further investigation. List of collection in DataBaseNest step is to find all collections in interesting database. We will use intro_db for further analysis
We can see 4 collections in intro_db and we will investigate iris collections Collection sizeTo check the size of the present collection, use count_documents() method
To find size of returned colelction – you can use standard len checking. Printing one document from collectionTo find any one documents in collection we can use method find_one()
It is clear what information is stored in this object. Printing all documents from collectionsIt is possible to take all documents from collection and analyse then later or print them or do any other actions with them with method find() To know the amount of documents after find() - it is possible to count length of the cursor, converted to a list, but it is important to make a clone of this cursor first, otherwise it will be used and you need to find new one
This operation will print all 150 documents Simple find some databaseWe can use method find() to specify what documents we need to find. Here I will show you the simplest search, for example for given variety field
This will print 50 lines with given variety.
|
Last 10 artitles
9 popular artitles
|
|
© 2020 MyCoding.uk -My blog about coding and further learning. This blog was writen with pure Perl and front-end output was performed with TemplateToolkit. |