If you’re not sure whether you have the MongoDB Database Tools/ mongoexport installed, try running the following command in your Terminal or Command Prompt to check: mongoexport -version The MongoDB Database Tools are a suite of command-line utilities for working with MongoDB. Mongoexport is part of the MongoDB Database Tools package. There’s also an -authenticationDatabase parameter for specifying the authentication database where the user has been created.Įxample: mongoexport -host= -port=37017 -username=homer -authenticationDatabase=admin -db=PetHotel -collection=pets -type=csv -fields=_id,name,type,weight -out=data/pets.csv Check for mongoexport If you omit the password parameter, you will be prompted for it. You can also use the -username parameter to specify the user name and -password to for the password. If you’re not using localhost, you can use the -host parameter to specify the host, and -port to specify the port. Results in the following CSV file: _id,weight,type,nameĪnd the following: mongoexport -db=PetHotel -collection=pets -type=csv -fields=type,name,weight,_id -out=data/pets.csvĭog,Jake,30,7 Access Control/Authentication They don’t have to be in the same order of the underlying document.įor example, this code: mongoexport -db=PetHotel -collection=pets -type=csv -fields=_id,weight,type,name -out=data/pets.csv You can switch the ordering of the fields to export. This resulted in the contents of the exported file looking like this: _id,name,type,weight Here’s what the pets_fields.txt file looked like for this example: _id The file that contains the fields must have the fields listed, one per line. mongoexport -db=PetHotel -collection=pets -type=csv -fieldFile=data/pets_fields.txt -out=data/pets.csv You can replace the -field parameter with the -fieldFile parameter to specify the name of a file that contains the field names that you want to export. Now when I open up the exported file, there are no column headers: 1,Wag,Dog,20ħ,Jake,Dog,30 Use a File for the Field Names To do this, use the -noHeaderLine parameter.Įxample: mongoexport -db=PetHotel -collection=pets -type=csv -fields=_id,name,type,weight -noHeaderLine -out=data/pets.csv You also have the option of exporting the file without column headers. You’ll notice that the exported file in the previous example included the column headers. OK, so all data is in the exported file as expected. Now let’s open up the exported file pets.csv to see what’s inside: _id,name,type,weight So we can see that there are 7 pets, all with the same fields that we specified in our export operation. Let’s verify that the export operation worked as expected.įirst, let’s check the original collection. If you don’t specify a file name, mongoexport writes data to standard output ( stdout). Specifies the exported file name and where it will be located. You can do so via the -fields parameter or the -fieldFile parameter (more on that later). When exporting to CSV, specifying the field names is a requirement. You need to list each one here, separated by a comma. We have the option of exporting all fields in the collection, or just some. Specifies the fields that we want to export. In this case we specify csv to export it to a CSV file. This parameter can alternatively be passed as -c (instead of -collection). In this case, the collection is called pets. Specifies the collection we want to export. This parameter can alternatively be passed using -d (instead of -db). In this case, the database is called PetHotel. Specifies the database that contains the collection we want to export. In this example I didn’t specify any host, port, authentication, etc, so it exports the collection from the MongoDB instance running on the default localhost port number 27017.īelow is an explanation of the parameters we supplied here. By the way, this assumes that there are no permission issues with writing a file to the specified location. If the folder doesn’t exist, it’s created. This exports a collection called pets from the PetHotel database to a file called pets.csv in the data/ folder. The following example code exports a collection from MongoDB: mongoexport -db=PetHotel -collection=pets -type=csv -fields=_id,name,type,weight -out=data/pets.csv a new Terminal or Command Prompt window).ĭon’t run mongoexport commands from the mongo shell. You need to run mongoexport commands from your system’s command line (e.g. The syntax for mongoexport goes like this: mongoexport -collection= This article shows you how to use mongoexport to export a MongoDB collection to a CSV file. The MongoDB Database Tools include a utility called mongoexport that enables you to export MongoDB data to a CSV or JSON file.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |