Mongoexport --fieldFile error: Failed: open fields.txt

I have been working on MongoDB Server 4.4 with Mongoshell, MongoDB Compass , MongoDB Cloud, MongoDB Database tools. Now the current installation is MongoDB Server 5.03. I have been facing some difficulties while working with old version of MongoDB.

Should I have to install MongoDB 5.03 along with Mongoshell, Compass, Cloud snd Database tools ?

HI @Arindam_Biswas2,

Can you provide more details on the difficulties you are encountering (for example, specific error messages or unexpected behaviour)?

For the classic mongo shell you should always install a version matching your server release – there will be a startup warning if a major version mismatch is detected.

The new MongoDB Shell, Compass, and Database Tools are independently versioned from the MongoDB server. I’d recommend installing the latest versions of these tools, particularly if you have upgraded to MongoDB 5.0. Older versions should still be compatible but will be missing support for newer server features.

Regards,
Stennie

Thank you for your reply. My MongoDB Server Version installed is 4.4.6 , Mongoshell Version installed is V4.4.6 and Compass Version installed is 1.28.1. Since MongoDB Server and Mongoshell are both having same version , there is no question of mismatch. My MongoDB Tool Version is 100. So, I need to install the latest version of MongoDB Tool.

Now, let me tell you the specific details I am encountering with my old version of MongoDB/ Mongo tools though I am not very sure whether there is any other specific reason behind it.
While I am exporting in csv format using - - fields option and --mongoexport to connect mongod running on local host : 27017, I am getting the output.

But , while I am using a file to specify a fields to export in csv format using – mongoexport to connect mongod runing on local host: 27017, I getting no output.
I am getting the error :" Failed: open fields.txt. The system can not find the file specified."
I shall be pleased if you let me know why it is so.

Please show the command you have used
You should use --fieldFile for passing a file

The --fieldFile option I use to export in csv format is
mongoexport --db=users --collection=contacts --type=csv --fieldFile=fields.txt --out=C:\opt\backups\contacts.csv
The database I created in mongoshell:
use users
db.contacts.insertMany( [
{ name: “Sophie Manroe”, address: “123 Example Road” },
{ name: “Charles Yu”, address: “145 Sample Street” }
] );

Hi @Arindam_Biswas2,

This error message indicates that fields.txt cannot be found in the current directory (or path to fields.txt, if one was specified).

It looks like you are following the mongoexport documentation for Use a File to Specify the Fields to Export in CSV Format, which includes an example you can copy to fields.txt:

name
address

If you only have a few fields to select, you can also provide a comma-delimited list with --fields=...:

mongoexport --db=users --collection=contacts --type=csv --fields=“name,address” --out=C:\opt\backups\contacts.csv

Regards,
Stennie

I want more clarification from your end. The following command
mongoexport --db=users --collection=contacts --type=csv --out=C:\opt\backups\contacts.csv will give the error message : Failed: CSV mode requires a field list.
Also, the following command
mongoexport --db=users --collection=contacts --type=csv --fieldFile=fields.txt --out=C:\opt\backups\contacts.csv will give error message : Failed: open fields.txt. The system can not find the file specified.

So, to get an output :
name
address
what mongoexport command should we need ? Please clarify.

Hi @Arindam_Biswas2,

Try:

mongoexport --db=users --collection=contacts --type=csv --fields=“name,address” --out=C:\opt\backups\contacts.csv

To use --fieldFIle fields.txt you need to create the file fields.txt with a list of fields (one per line) as per the example in the docs.

Regards,
Stennie

1 Like

I am using the following command-line

mongoexport --db=users2 --collection=contacts2 --type=csv --fieldFile=fields.txt “name”, “address” --out=C:\opt\backups\contacts2.csv.
I am getting the following error
error parsing command line options: error parsing positional arguments: provide only one MongoDB connection string. Connection strings must begin with mongodb:// or mongodb+srv:// schemes

When you pass fieldFile there is no need to pass field names as parameters again

Please remove name,address from the command
Have you added these two fields in your fields.txt as Stennie suggested?

name: “Sophie Manroe”, address: “123 Example Road”
Here the field name is name, address. Fields values are : “Sophie Manroe”, “123 Example Road” in one line.
You advise me to add two fields in fields.txt. Does it mean : --fieldFile=fieldstext – fields=“Sophie Manroe”, “123 Example Road”

Just add in the file
name
address

The document is quite clear

For CSV exports only, you can also specify the fields in a file containing the line-separated list of fields to export. The file must have only one field per line.

For example, you can specify the name and address fields in a file fields.txt :

name
address

As per your advice my collection documents
db.contacts.insertMany([
{ name: “Sophie Manroe” } ,
{ address: " 123 Example Road" }
]);

This document is made as you advised to create field value with the field names , name and address so that there can be only one field value with the field names , name and address.

For csv export , we can use the following command-line

mongoexport --db=users --collection=contacts --type=csv --fieldFile=fields.text --out=C:\opt\backups\contacts.csv

Is the command line of mongoexport to create csv output file correct ?

Or anything else we have to add in between fieldFile=fields.txt and --out=C:\opt\backups\contacts.csv
in the command- line of mongoexport to create csv output file ?

I shall be pleased if you specifically reply with my details collection documents which I mentioned in my post.
Regards ,
Arindam Biswas

You command looks okay. If still unsure you will find the documentation with examples at https://docs.mongodb.com/database-tools/mongoexport/.

Have you tried your command?

What did you get?

Can you share the content of the file fields.text?

Following are the details of my database, collection , field_name.text( field.text), mongoexport command - line option , error along with screen shots.
Database and collection in mongoshell

use customers
switch to customers
show collections
info
db.info.insertMany( [
{ Name: “Aniruddha Ray”, address: " Golf green", ph: 9811111111 },
{ Name: “Rahul Chiudhury”, address: “Golf course”, ph: 7922222222 }
] );
field_name.txt file(field.text)
Name
address
phone
Field directory where I saved field_name.txt file > C:\Users\Arindam PC\Documents\field_name.txt
mongoexport command-line option to get output in csv format
mongoexport - - db=customers - - collection=info - -type=csv - -fieldFile=C:\Users\Arindam PC\Documents\field_name.txt - -out=C:\opt\backups\info.csv
Error: 2021-08-19T14:36:18.090+0530 error parsing command line options: error parsing positional arguments: provide only one MongoDB connection string. Connection strings must begin with mongodb:// or mongodb+srv:// schemes



field_name

It is a syntax issue
There are spaces in your command
Put the command in a notepad and remove the spaces before each parameter and run the command again.It will work

    • db is not same as --db

I have not used any space in the command while usuing the --mongoexport command in mongo shell. Please see the screen shot. In the forum I use space intentionally to clarify two - - lines. I use --db not - -db in mongo shell.
Why did you advise me to put the command on notepad and remove the spaces before each parameter and run the command again ?



field_name file

He did because you have an error and we are trying to help you figure out what you do wrong. In this case the error seems to be misleading as it says to specify only 1 connection string. I do not see any on your command. And according to https://docs.mongodb.com/database-tools/mongoexport/ you do not need to specify any when connecting to localhost at the default port. By looking in more details, I see that your --fieldFile argument has a space in it. You should put it in double quotes.

2 Likes

Thanks a lot. Atlast I get the solution. I have been stuck up since long. So many people helped me. Let me thank to all again.
Now, what was the issue with respect to my last posting ? I did not use the double quote (" ") both in field_name.txt file and output file.
–fieldFile=“C:\Users\Arindam PC\Documents\field_name.txt”
–out=“C:\opt\backups\info.csv”
I also attach the screen shot of complete command-line option of mongoexport.

Regarding my last query : Why did you advise me to put the command on notepad and remove the spaces before each parameter and run command again ?
I understand that he was trying to help me to figure out what exactly wrong was. Since, I was confident that the issue was not due to space problem I replied that way. I also attached the screen shot and pointed out to verify whether the same space issue also exists in the screen shot also.


Anyway , I am sorry for my reply if it sounds harsh.

The command in your screenshot did not have extra spaces but we were thrown off because you wrote

You had an extra space between every pair of dashes. You also add and extra space before db= and collection=.

And why Notepad. Because it is easier to edit a command with an editor rather than the command line.