Syntax missing ; before statement in mongoexport - mongodb

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.

Related

MongoDB replSet exception [duplicate]

The issue seems straight forward. I have a database (test) and a collection called (users) so I run the command:
mongoexport -d test -c users -o output.json
However I get the below error:
As per what I have figured out till now over the internet, this may have something to do with the file path but I am unsure as how to amend this as I never mess with PATH variable due to a bad experience...
You don't run mongoexport from the mongo shell, you have to run it from the OS shell (same as you run mongo)
mongoexport is not a Mongo shell command, it's an operating system command.
Just like you run mongo.exe to start the shell from OS prompt, you should run mongoexport the same way from OS prompt. Example:
c:\mongodb\bin>mongoexport --db ventfeed --collection users --out C:\temp\contacts.json
Thanks

How to export all collections in MongoDB

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.

export a csv from mongodb

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

Command fails in script, works in command line

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\"}'"

Export one object with mongoexport, how to specify _id?

I'm trying to export just one object with mongoexport, filtering by its ID.
I tried:
mongoexport -d "kb_development" -c "articles" -q "{'_id': '4e3ca3bc38c4f10adf000002'}"
and many variations, but it keeps saying
connected to: 127.0.0.1
exported 0 records
(and I'm sure there is such an object in the collection)
In mongo shell I would use ObjectId('4e3ca3bc38c4f10adf000002'), but it does not seem to work in the mongoexport query.
I think you should be able to use ObjectId(...) in the query argument to mongoexport:
mongoexport -d kb_development -c articles -q '{_id: ObjectId("4e3ca3bc38c4f10adf000002")}'
If that does not work, you can use the "strict mode" javascript notation of ObjectIds, as documented here:
mongoexport -d kb_development -c articles -q '{_id: {"$oid": "4e3ca3bc38c4f10adf000002"}}'
(Also note that strict mode JSON is the format produced by mongoexport)
You have to specify the _id field by using the ObjectId type. In your question it was specified as a string.
CODE ::
mongoexport -h localhost -d my_database -c sample_collection -q '{key:ObjectId("50584580ff0f089602000155")}' -o my_output_file.json
NOTE :: dont forgot quotes in query
My MongoDB verion: 3.2.4. when I use mongoexport tool in mongo shell:
NOT WORK:
-q '{"_id":ObjectId("5719cd12b1168b9d45136295")}'
-q '{_id: {"$oid": "5719cd12b1168b9d45136295"}}'
WORKs:
-q "{_id:ObjectId('5719cd12b1168b9d45136295')}"
- Though in mongo doc , it says that
You must enclose the query in single quotes (e.g. ') to ensure that it
does not interact with your shell environment.
- But, single quote(') does not work! please use double quote(")!
for mongoexport version: r4.2.3
mongoexport -q '{"_id": {"$oid": "4e3ca3bc38c4f10adf000002"}}'
and for a nested field
mongoexport -q '{"_id": {"$oid": "4e3ca3bc38c4f10adf000002"}}' --fields parentField.childField
You do not have to add ObjectId or $oid as suggested by answers above. As has been mentioned by #Blacksad, just get your single and double quotes right.
mongoexport -d kb_development -c articles -q '{_id:"4e3ca3bc38c4f10adf000002"}'
many of the answers provided here didn't work for me, the error was with my double quotes. Here is what worked for me:
mongoexport -h localhost -d database_name -c collection_name -q {_id:ObjectId('50584580ff0f089602066633')} -o output_file.json
remember to use single quote only for the ObjectId string.