Importing a CSV to Mongo on a server - mongodb

I am having trouble importing a CSV to a MongoDB instance deployed on a server. I was able to use mongoimport to load data to a local instance using the following
mongoimport -d Josh -c diagnosis -- type csv --file\\location\name.csv --headerlin -- ignoreblanks
I have tried to load data to Mongo located on a server through cmd:
C:\Program Files\MongoDB 2.6 Standard\bin>mongoimport --host xxx.xx.x.xxx --db Josh -c diagnosis -- type csv --file\\location\name.csv -- headerline --ignoreblanks
but I get the following error
"Error Parsing Command Line: too many positional options"
What does “too many positional options” mean when doing a mongoexport?
One posting says it is because of black fields in the CSV, however, the --ignoreblanks should take care of blank fields, as it worked on the local instance.

Please add space after --file and no space before type
C:\Program Files\MongoDB 2.6 Standard\bin>mongoimport --host xxx.xx.x.xxx --db Josh -c diagnosis --type csv --file location\name.csv -- headerline --ignoreblanks

Related

How to restore gzipped Monogdb single collection?

Hye everyone, I am new to mongodb I dumped one of my collection using following command mongodump --db somedb --collection somecollection --out - | gzip > collectiondump.gz given on the link now when I try to restore it using mongorestore it gives Error dont know what to do with file.
kindly help me I used this way because the answer in the given link got too much upvotes
Error image
You can just add gzip to the restore command:
mongorestore --gzip --db projectx userprofiles/
Okay I have got my answer first of all I converted the extension of my gunzipped file to bson by using command
gunzip -c userprofiles.gz > users.bson
after that I used mongorestore command
mongorestore --db projectx userprofiles/
Thats it

Cannot import example dataset (the system cannot find the specified file)

I am following the example given on MongoDB's website here, but I am running into trouble when trying to import sample data.
When running the command
mongoimport --db test --collection restaurants --drop --file primer-dataset.json
I get the error:
Failed: open primer-dataset.json: The system cannot find the file specified
The problem is, I am not sure what directory MongoDB expects this file to be in. I tried placing it in data/db, but that did not work. Note that I am only using default settings.
I know this is a somewhat trivial question and I feel stupid for asking it but I can not find documentation on this anywhere. Where is MongoDB expecting import files?
MongoDB expects the file to be in the directory from where you are running the command mongoimport.
If you place your file under data/db then set mongodb path as global environment variable and execute the command from data/db directory.
Additionally if you have security enabled for your mongodb then you need to execute command as below
mongoimport --username admin --password password --db test --collection restaurants --drop --file primer-dataset.json
here admin is the user authorized to perform db operations for test database and restaurants is the collection name.
For Windows!
Save file using notepad++ in .json format at MongoDB/bin where mongoimport command is present.
Simple notepad has trouble doing it.
It happened to me as well. The issue was that though the file was visible as restaurants.json actually the file was restaurants.json.json (since saved in JSON format). The issue was resolved after properly changing the name.
i have trouble like you, check you path to file, mongoimport.exe and your file may be stay in another folders.
use mongoimport -d test1 -c restaraunts companies.json for import to mongodb.
Check the filename extension, and make sure it's a ".json" file;
After this I successfully run the
mongoimport --db test --collection restaurants --drop --file [path\to\Json file]
command;
In my case, I removed the --drop parameter and it worked perfectly. I guess, it is throwing this error:
Failed: open paht/file-name.json: The system cannot find the file specified.
because the collection it wants to drop is not available, because I have not created any before.
you must copy your json file into C:\Windows\System32 and write this command on cmd:
mongoimport --db test --collection mongotest --type json --file yournamefile.json

Inserting JSON document into mongo error

I'm trying to insert the TWDS1E1.json file into mongodb through the command prompt:
db.collections.insert( TWDS1E1.json )
But getting the error:
TWDS1E1.json is not defined.
Mongo is not my thing, what am I doing wrong here?
In command prompt whose directory path is the path where mongoimport.exe is available type the commands
For normal JSON
mongoimport -d test -c docs --file example2.json
For array type JSON
mongoimport --jsonArray -d test -c docs --file example2.json
Please see docs for more information
You cannot use the collection.insert() command to insert a file.
insert() is used to insert actual objects, e.g.
db.myCollection.insert({"name":"buzz"});
To bulk load a JSON file, use mongoimport

how to import csv file in mongodb

I have a csv file containing following data and want to import it in mongodb
ID;"AdmissionID";"SeatNo";"RegistrationNo";"ResultDate";"ResultStatusId"
1;12;"2323";"23";07-05-2013;1
2;23;"35";"32";10-05-2013;5
this data is to be imported to mongodb 2.2. I'm using following command:
mongoimport -d test -c exam --type csv --headerline <f:\exam.csv
when used i get following error
SyntaxError: missing ; before statement (shell):1
please help me to find out the error
This should do the trick easily. More HERE.
mongoimport -d mydb -c collectionName --type csv --file myfile.csv --headerline
Your problem is the <f:\exam.csv bit, which is not properly escaped by the way it looks
> --headerline
> If using “--type csv” or “--type tsv,” use the first line as field names. Otherwise, mongoimport will import the first line as a distinct
> document.
Please try this line of code
C:\Program Files\MongoDB\Server\3.2\bin>mongoimport -d pravin -c FOC --type csv
--file D:\Script\ImportData\FOC.csv --headerline
2016-08-10T15:42:38.685+0530 connected to: localhost`enter code here`
2016-08-10T15:42:38.758+0530 imported 13 documents
I solved by opening the .csv file in Excel (File -> Options -> advanced) then unchecking the box "uses system separation" and then removing the comma from the box below and then saving again in .csv.
So there will not be any commas in the .csv file and the formatting of JSON in MongoDB will be right.
Run mongoimport from the system command line, not the mongo shell.
Ref - https://docs.mongodb.com/manual/reference/program/mongoimport/

Getting an assertion error in mongoexport command in Mongodb.

I am getting an error after executing this command:
mongoexport --db records --collection source_list --csv --out C:\bcopy.csv
record is my DB n source_list is my collection
It displays this message:
assertion: 9998 you need to specify fields
I also tried to specify fields but it is giving me the same error.
What changes should i make in the command to get a backup of my collection or is there any other way to do so ?
Here's sample command that specifies fields to export:
mongoexport -h 127.0.0.1 --port 27018 --db mydb --collection system.profile --csv --out profile.csv --fields ns,millis,numYield,nscanned
In my case --headerline helped. I had around 60 columns, enumerating them with -f would be quite cumbersome.
--headerline
If using “--type csv” or “--type tsv,” use the first line as field names. Otherwise, > mongoimport will import the first line as a distinct document.
Seems like you should be using -f paramater to choose the fields that will be exported to csv file. There is a bug reported for this case to change the explanation as the error message is not informative enough.
https://jira.mongodb.org/browse/SERVER-4224