Extracting Specified Columns from MongoDB in csv's Using Python

Hi This is Murtuza Kantawala from India
I have a collection in Mongodb
i need to write a python Script to Extract data from it
example
"_id ":abc
“address:” {
“building”:
" area :

So i need address also and building and area also to get extracted in python and i want to write function so that it is easily calleble not the hard code

Hi Murtuza, I would use projection for this. If you are using python and Pymongo you can simply do this.

from pymongo import MongoClient
client = MongoClient('mongodb://localhost:27017')
db = client['database_name']
db.coll.find({}, {"_id:0", "address: 1", "building: 1", "area":1})

Of course you will have to change your mongodb connection string to match your database. But This will find all documents in a collection and then only return the building and area fields.

Then you can create a function to pass in the parameters that you would like to change ie (db name, collection, fields that need to be projected)

wait i am sending you screen shot so you can understand better

MongoDB Compass - cesspool-ft7rs.mongodb.net 27-10-2021 06_13_23

hi @tapiocaPENGUIN as you can see the 3 images there are nested fields
so now how to extract data from these nested field i have tried and converted into CSV but data not extracted the rows are empty

@tapiocaPENGUIN

MongoDB Compass - cesspool-ft7rs.mongodb.net 27-10-2021 06_16_53

The projection should still work, but you will just need to use a “.” separated format to project the nested fields.

db.coll.find({}, {"_id:0", extras.basicInformation.address: 1", "building: 1", "area":1})

Here is the MongoDB documentation about projection with imbedded documents:


As you can see i get output in dataframe like this
but i dont want the output like this i want only village name
eg: PIMPALGOAN
BELGAV
… etc etc
so please reply how to do this

please @tapiocaPENGUIN help me its really URGENT

hello is anybody know the solution for this please help me out urgently

If you have the data that you need in a dictionary, then you should just be able to find a specific key-value the normal python way. (Although it looks like you are using pandas and I’m no expert in pandas). But I would try something like this:

print(df['address']['village'])
1 Like