Mongoexport to csv seems to read records but the data fields are empty

I’m learning how to use Mongdb and Atlas.
I successfully created documents in my Cluster0.
Using the mongoexport command I tried to export the data to a csv file but only the field column titles get created, no data. When executing the command, it says it exported 94 records, a file is created …but the data is missing.
However, if I run the mongodump command with similar syntax, that execution states it exported 94 records, a BSON file is created and and it works as expected. I used an online converter and converted the binary to csv so I do see the data I expect, but I’d rather not have that extra step with a converter.

What am I doing wrong with the mongoexport command?

I’m using Windows 10 and a CMD window.

Here is the cmd window input/output.

C:\util\mongodb-database-tools-windows-x86_64-100.7.3\bin>mongodump --uri mongodb+srv://kurt:xxxx@cluster0.286gegr.mongodb.net/ur_sensors --collection Dissolved_DO --out bin/exptest3.bin
2023-07-18T18:25:00.587-0500    writing ur_sensors.Dissolved_DO to bin\exptest3.bin\ur_sensors\Dissolved_DO.bson
2023-07-18T18:25:00.645-0500    done dumping ur_sensors.Dissolved_DO (94 documents)
C:\util\mongodb-database-tools-windows-x86_64-100.7.3\bin>
C:\util\mongodb-database-tools-windows-x86_64-100.7.3\bin>mongoexport --uri mongodb+srv://kurt:xxxx@cluster0.286gegr.mongodb.net/ur_sensors --collection Dissolved_DO --fields Temp,DO,Date,Time,HWINFO,BatVolt,Comments --type=csv --out outtestX.csv
2023-07-18T18:25:16.310-0500    connected to: mongodb+srv://[**REDACTED**]@cluster0.286gegr.mongodb.net/ur_sensors
2023-07-18T18:25:16.451-0500    exported 94 records
Here is a copy of a few of the mongodb documents, it is just JSON data: 
{"_id":{"$oid":"64b6c45d54deb6b6af3b6e46"},"date":{"$date":{"$numberLong":"1689699421390"}},"requestBody":{"Temp":"28.50","DO":"7258","HWINFO":"DO2 Sensor SolarLTE1 sw:1.0.1","BatVolt":"4366.64","Comments":"Misc info","Date":"07/17/2023","Time":"17:14"}}
{"_id":{"$oid":"64b6c463867a358dbda0e8f0"},"date":{"$date":{"$numberLong":"1689699427679"}},"requestBody":{"Temp":"29.50","DO":"7076","HWINFO":"DO2 Sensor SolarLTE1 sw:1.0.1","BatVolt":"4331.18","Comments":"Misc info","Date":"07/17/2023","Time":"17:29"}}
{"_id":{"$oid":"64b6c46ae8ccbd12e9d74fb5"},"date":{"$date":{"$numberLong":"1689699434014"}},"requestBody":{"Temp":"-127.00","DO":"4967","HWINFO":"DO2 Sensor SolarLTE1 sw:1.0.1","BatVolt":"4357.77","Comments":"Misc info","Date":"07/17/2023","Time":"17:44"}}

The resulting csv file just has one line with 6 columns: Temp DO Date Time HWINFO BatVolt Comments.

Again….what is mongoexport expecting in order to do this correctly? I kinda suspect my data is causing this, maybe the quotes but I think it is proper JSON? I’d be surprise though to think Mongo was this sensitive to data.

1 Like

Hello @kurt_h ,

I tried your query and updated it as below to get the expected result.

mongoexport --uri mongodb+srv://kurt:xxxx@cluster0.286gegr.mongodb.net/ur_sensors --collection Dissolved_DO --fields requestBody.Temp,requestBody.DO,requestBody.Date,requestBody.Time,requestBody.HWINFO,requestBody.BatVolt,requestBody.Comments --type=csv --out outtestX.csv

This was done because the fields you are trying to access is available under requestBody object and this is known as Dot notation used to access fields of array.

Let me know if you get any issues/queries, will be happy to help! :slightly_smiling_face:

Regards,
Tarun

1 Like

Tarun,

Excellent! That was the problem, another step in learning Mongodb. Thank You very much.

Kurt_H

2 Likes

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