How to export a collection from mongo using terminal - mongodb

I want to export the whole data from one of my collections in mongodb to a csv file.
This is how my db looks like:
"Tags",
"checkouts",
"imports",
"products"
I am trying to export "checkouts".
I am connected to the db through the terminal and I tried the following commands:
mongoexport --db nameofdatabase --collection checkouts --out coll.json
mongoexport --db nameofdatabase --collection checkouts --type=csv --fields --out /opt/backups/contacts.csv
The error message I get after running both commands command is : "Unexpected identifier"

MongoExport cannot be used in the mongo shell. It is a terminal command like mongo.

Related

mongoexport / mongodump with option --uri

During my studying of MongoDB I tried to dump a collection from training course's mongo database.
I tried both mongodump and mongoexport.
My shell command is like
mongoexport --uri="mongodb://<my-username>:<my_password>#<replica-1>.mongodb.net:27017,<replica-2>.mongodb.net:27017,<replica-3>.mongodb.net:27017/<database>?replicaSet=<replica-set>" --collection=<collection-name> --out=<collection-name>.json --ssl
When I run the command, nothing happened: no response, no new local files, no errors, only cursor flashes.
How can I export collections by the uri?
--forceTableScan
option can be used along with mongodump & --uri option
mongodump --uri
mongodb+srv://<user_name>:#.mongodb.net/db_name
--forceTableScan --collection <collection_name> --out ./dump/

Syntax error:missing ; before statement #(shell):1:15 [duplicate]

I'm working with mongodb 2.4.3 and I cannot get mongoexport to work. The error message I get with each command is:
"JavaScript execution failed: SyntaxError: Unexpected identifier"
At first I thought the problem was with my query parameters, which were long. But find() worked fine with the query so I know that the syntax is ok. I then created a new collection using the query and tried exporting the collection with the following:
mongoexport --db Oct2012 --collection sept8subset --csv --fields "text","created_at","user.screen_name","user.followers_count" --out sept8.csv
mongoexport --db Oct2012 --collection sept8subset --csv --fields text,created_at,user.screen_name,user.followers_count --out sept8.csv
mongoexport -d Oct2012 -c sept8subset --csv --fields text,created_at,user.screen_name -o sept8.csv
mongoexport --db Oct2012 --collection sept8subset --dbpath ~/db (should need dbpath as mongod instance is running)
mongoexport --db OCt2012 -collection sept8subset -o sept8.txt
mongoexport --db Oct2012 --collection sept8subset
In each case, I get "JavaScript execution failed: SyntaxError: Unexpected identifier". Where could that SyntaxError be??
The collection I'm trying to copy has 50,339 objects. In case it is just too big for mongoexport to handle, I took 5 documents out of the collection to make a new collection. I then tried to export them using same command structure as above. I still get the same error message.
Now I'm wondering if the problem is that mongoexport can't work with data involving dates. MongoDB documentation states that I may want to write my own export script using a client driver.
Does anyone have an idea of what my problem is here? Many thanks if you can help out.
I found out that running mongoexport from the mongo client--as I was doing--is incorrect. Mongoexport is executed at the command prompt.
I ran into this too and I solved it executing the command from the path/bin where 'mongoexport' is located.In my case :
my_path: /usr/local/Cellar/mongodb/2.4.4-x86_64/bin
"mongoexport -d book -c shelf"
'mongod' is located one step upper from 'mongoimport' , 'mongoexport', etc...

can not find unexpected undentifier in mongo db export query

I am writing this query in mongo shell
mongoexport --db database_name --collection collection_name --out filename.json
query to export but the error is
SyntaxError: unexpected identifier
can anybody help me ?
Mongoexport is a terminal command line utility to export data, not mongoshell functional directive.
Try running the same in terminal.
$ mongoexport --db database_name --collection collection_name --out filename.json
Make sure you have all the utilities that come with mongo added to your path.

Mongodb export to csv - mongoexport is failing to export - Help Required

show dbs
TestDB 0.078GB
use TestDB
switched to db TestDB
show collections
Employee
MyCollection
db.MyCollection.find()
{ "_id" : ObjectId("55cb93c412a2139d0a6078a1"), "MyNames" : "Sanal" }
mongoexport --db TestDB --collection MyCollection --type=csv --fields MyNames --out D:\contacts.csv
2015-08-22T00:44:20.380+0530 E QUERY SyntaxError: Unexpected identifier
>
From that error message it looks like you are still in the mongo shell. mongoexport is a separate utility. You need to exit the mongo shell, the command is just exit. Then enter your mongoexport command again on the command line.

mongoexport syntax error message

I'm working with mongodb 2.4.3 and I cannot get mongoexport to work. The error message I get with each command is:
"JavaScript execution failed: SyntaxError: Unexpected identifier"
At first I thought the problem was with my query parameters, which were long. But find() worked fine with the query so I know that the syntax is ok. I then created a new collection using the query and tried exporting the collection with the following:
mongoexport --db Oct2012 --collection sept8subset --csv --fields "text","created_at","user.screen_name","user.followers_count" --out sept8.csv
mongoexport --db Oct2012 --collection sept8subset --csv --fields text,created_at,user.screen_name,user.followers_count --out sept8.csv
mongoexport -d Oct2012 -c sept8subset --csv --fields text,created_at,user.screen_name -o sept8.csv
mongoexport --db Oct2012 --collection sept8subset --dbpath ~/db (should need dbpath as mongod instance is running)
mongoexport --db OCt2012 -collection sept8subset -o sept8.txt
mongoexport --db Oct2012 --collection sept8subset
In each case, I get "JavaScript execution failed: SyntaxError: Unexpected identifier". Where could that SyntaxError be??
The collection I'm trying to copy has 50,339 objects. In case it is just too big for mongoexport to handle, I took 5 documents out of the collection to make a new collection. I then tried to export them using same command structure as above. I still get the same error message.
Now I'm wondering if the problem is that mongoexport can't work with data involving dates. MongoDB documentation states that I may want to write my own export script using a client driver.
Does anyone have an idea of what my problem is here? Many thanks if you can help out.
I found out that running mongoexport from the mongo client--as I was doing--is incorrect. Mongoexport is executed at the command prompt.
I ran into this too and I solved it executing the command from the path/bin where 'mongoexport' is located.In my case :
my_path: /usr/local/Cellar/mongodb/2.4.4-x86_64/bin
"mongoexport -d book -c shelf"
'mongod' is located one step upper from 'mongoimport' , 'mongoexport', etc...