Prepare data to Charts


I’m new to MongoDB and I have the challenge to convert CSV files to the below format.

 $category_amount = array(
        array("UserName"=>"User1","Leads"=>32000,"Closed Won"=>20000,"Closed Lost"=>12000),
        array("UserName"=>"User2","Leads"=>43000,"Closed Won"=>36000,"Closed Lost"=>7000),
        array("UserName"=>"User3","Leads"=>54000,"Closed Won"=>39000,"Closed Lost"=>15000),
        array("UserName"=>"User4","Leads"=>23000,"Closed Won"=>18000,"Closed Lost"=>5000),
        array("UserName"=>"User5","Leads"=>12000,"Closed Won"=>6000,"Closed Lost"=>6000),

CSV Sample:

username,stage_en,price <== Columns Header
Ashraf Shehadeh,Closed Lost,58
Mohammad Allan,Closed Lost,580
Jamil  Mahmoud ,Closed Lost,556.8
Ashraf Shehadeh,Closed Won,406
Leen  Ibrahim,Closed Lost,0
Ashraf Shehadeh,Closed Lost,928
Mohammad Allan,Closed Lost,928
Mohammad Allan,Closed Won,522
Leen  Ibrahim,Closed Lost,0
Agent 16,Closed Lost,0

What I need is to convert the list to a formatted array to be able to apply it as a Chart datasource.


Hi @Ahmad_Abujoudeh and welcome to the MongoDB community!!

If I try to understand correctly, are you trying to import a CSV file to the data source in MongoDB charts?
You can import the csv file directly to the collection using CSV import using mongoimport and further use the collection as datasource.

mongoimport --db=users --type=csv --headerline --file=/opt/backups/contacts.csv

For further understanding, please refer to the documentation on Data Source in MongoDB charts which would be a good reference for more details.

Let us know if you have any further queries.

Best Regards

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.