Can someone please tell me how to export all collections (into JSON Format) at a time through Command line
Steve's script generates the files with quotes at the ends. For this to not happen, the pattern is improved.
Improving the above script
#!/bin/bash
DB=$1
Collections=$(mongo localhost:27017/$DB --quiet --eval "db.getCollectionNames()" | sed 's/[^"[:alnum:]]/ /g')
for collection in $Collections; do
mongoexport -d $DB -c $collection -o ./$collection.json
done
Generally you want to use mongoexport or mongodump but each of those only works on a single collection.
To get all collections and export them all you need to run some type of script that can locate and then loop through everything.
On linux it would be something like this:
#!/bin/bash
DB=$1
Collections=$(mongo localhost:27017/$DB --quiet --eval "db.getCollectionNames()" | sed 's/,/ /g')
for collection in $Collections; do
mongoexport -d <DATABASE_NAME> -c $collection -o $collection.json
done
You'll need to this into a script file and then run it.
I haven't tested the above myself but it is hopefully error free.
Related
I am trying to export mongodb collection with:
mongoexport -d InventoryPLC -c Spectra -o spectra.json;
but it is giving me error:
Syntax missing ; before statement
I search on mongodb website and other reference websites:
mongoexport -d InventoryPLC -c Spectra;
I want to export collection.
I checked stackoverflow examples and mongodb documentation:
mongoexport -d InventoryPLC -c Spectra -o spectra.json;
I want exported collection, but have had no luck.
Appreciate the help.
You are probably trying to run mongoexport from inside the Mongo shell. That command must be executed outside it, from your OS command shell.
Is there a way to write the output of a MongoDB find() query to a file just by simply using a Linux shell command or running a script?
Right now I have to manually type in step-by-step. Example:
$ mongo
> use owndb
> db.CollectionName.find(<query>) ### and then copy and paste the result on a text editor
You may try this:
mongo --quiet dbname --eval 'printjson(db.collection.find().toArray())' > output.json
You can use mongoexport for that.
Example:
mongoexport -d dbname -c collection --jsonArray --pretty --quiet --out output.json
I'm trying to export an entire MongoDB collection sorted by some of the fields. I'm led to believe that the following should work:
$ mongoexport --csv -d my_db -c my_collection -f field1.subfield,field2.subfield -o d.csv -q '{$query:{},$orderby:{"field1.subfield":1}}'
Unfortunately, this only exports one record in the collection (there are 18478 records) and the data exported is blank. Leaving the $orderby blank, like so,
$ mongoexport --csv -d my_db -c my_collection -f field1.subfield,field2.subfield -o d.csv -q '{$query:{},$orderby:{}}'
, exports the whole collection the way I want, so clearly the orderby clause is wrong. What am I doing wrong?
mongoexport utility does not expect you to sort data.
I have two collections in my mongodb namely
1.companies
2.contacts
Both the companies and contact collection are interlinked. I want to export a particular companies contact into a csv. I have tried a mongo export command as follows
mongoexport --csv -d dbname -c contacts
-q {"employment_details.company_id":ObjectId("50926cff9fe3125819006dc7")};
-f {"first_name","last_name","title"} -o export.csv
I get a error as follows
SyntaxError: missing ; before statement (shell):1.
Please help me. Thanks in Advance
There could be a couple of things going on here. First, are you running mongoexport from the command line or from the mongo shell? The mongoexport command is run from the command line.
Secondly, you need to properly format the query and field parameters. You could enclose the query with single quotes, and the filed name is not a JSON document, but just a list of fields.
This would look like the following from the command line:
mongoexport --csv -d dbname -c contacts -q '{"employment_details.company_id":ObjectId("50926cff9fe3125819006dc7")}' -f "first_name","last_name","title" -o export.csv
The following query will work if it is running from commandLine
mongoexport -h host -d dbname -c contacts --csv -q '{"employment_details.company_id":ObjectId("50926cff9fe3125819006dc7")}' -f first_name,last_name,title -o export.csv
I have a simple script to get an export from MongoDB:
#!/bin/sh -x
QUERY="'{ \"type\":\"listing\" }'"
mongoexport --db event --collection listing --query $QUERY --fields type,name --csv
(I'm using the -x switch for debugging purposes)
Here's the output when I run the script:
$ ./simple_query.sh
+ QUERY='{ "type":"listing" }'
+ mongoexport --db event --collection listing --query '{ "type":"listing" }' --fields type,name --csv
ERROR: too many positional options
However, if I merely copy and paste the mongoexport line to the prompt, it works! What's going on?
(Using MongoDB 2.0.0)
Are you using older version of mongodb? If so maybe it's this bug. It is fixed on 2.0.0-* versions.
Edit: Actually this might be different problem. You can fix it by removing the spaces on the query. Spaces seem to make the shell think it is actually multiple arguments:
QUERY="'{\"type\":\"listing\"}'"