mongoimport from CSV file not assigning object ID's properly - mongodb

I successfully followed commands to import from csv file to mongodb using CLI.
mongoimport --host myhost --ssl --username my_username --password my_password --authenticationDatabase admin --db my_db --collection addresses --type csv --fields Geocords,Display_lat,Display_lng,street_address,added_by_user --file exportdir2/MFCustomerList.csv
Now the object ID field is not something I have in my file. I expected each row to get its own object ID created on its own as I upload. However instead of just having an object id in there as so: id_:"kalaklakp", each row has something like this in there: id_:ObjectId("gaafagafagafs")
This is causing major problems especially for my Parse instance , It is not able to identify these objects as valid.

Related

Is there any way to import JSON zip file into mongodb using mongoimport?

I have created a zip file from a large JSON file(containing json array). I want to use this zip file in mongoimport command. Is it possible to import this zip file in mongodb using mongoimport command?
COMMAND:
mongoimport --db test --collection inventory ^ --authenticationDatabase admin --username <user> --password <password> ^ --drop --file ~\downloads\inventory.crud.json.zip --jsonArray
OUTPUT:
Failed: error reading separator after document #1: bad JSON array
format
Since this is a zip file it does not find a valid json array. Is there a way to unzip file in mongoimport command?

Cant import data into mongo database using mongoimport

I can't import data into my existing database located on mongoDBAtlas. I installed and connected robomongo with mongoDBAtlas for working with atlas.
I created new database jasper and collection User in robomongo then
I created user.json file in my project where are stored my data.
I followed tutorial on https://docs.atlas.mongodb.com/import/mongoimport/ - how to use mongoimport with mongodb.
Here is my command, Im typing in terminal:
mongoimport --uri mongodb://Morty:<PASSWORD>#jasper-shard-00-00-mrihb.mongodb.net:27017/jasper?ssl=true&replicaSet=jasper-shard-0&authSource=admin --collection User --drop --file ./src/data/user.json --jsonArray
that give me an error:
[1] 40930
[2] 40931
-bash: --collection: command not found
[2]+ Done replicaSet=jasper-shard-0
KSC1-LMC-K00587:Interview-test-part-one marze$ 2017-10-15T10:38:35.209+0200 no collection specified
2017-10-15T10:38:35.209+0200 using filename '' as collection
2017-10-15T10:38:35.209+0200 error validating settings: invalid collection name: collection name cannot be an empty string
2017-10-15T10:38:35.209+0200 try 'mongoimport --help' for more information
If I run mongoimport for localhost it works perfectly.
Where should be the problem ?
Solution:
-use quotes for uri param.
mongoimport --uri "mongodb://Morty:<PASSWORD>#jasper-shard-00-00-mrihb.mongodb.net:27017/jasper?ssl=true&replicaSet=jasper-shard-0&authSource=admin" --collection User --drop --file ./src/data/user.json --jsonArray

I want to import the json file only if they don't exist

I am using mongo 3.4
I want to import json file from json array to mongod using bash script, and I want to import the json file only if they don't exist. I tried with --upsert but it does not work.
Is there any easy way to do it? Thanks
mongoimport --db dbName --collection collectionName --file fileName.json --jsonArray --upsert
mongoimport -d dbName -c collectionName jsonFile.json -vvvvv
Even though the output of mongoimport says that n of objects were imported, the exsiting document with same data has not been overwritten.
if use --upsert it will update the existing document.
Found similar discussion here

Upload Data into MongoLab database from terminal

I'm having trouble figuring out how to upload csv data to my MongoLab database. From my terminal I have used
sudo mongoimport --db heroku_hkr86p3z -u <dbusername> -p <dbpassword> --collection contributors --type csv --headerline --file /Users/tonywinglau/Desktop/independent-expenditure.csv
and
sudo mongoimport --host mongodb://<username>:<password>#ds035310.mlab.com:35310/heroku_hkr86p3z --db heroku_hkr86p3z -u <username> -p <password> --collection contributors --type csv --headerline --file /Users/tonywinglau/Desktop/independent-expenditure.csv
both of which respond with
Failed: error connecting to db server: no reachable servers
imported 0 documents
From what I have read it might be something to do with my 'mongo config' file (I can't find it if it does exist) being set to only connect with localhost? How do I import data directly into my mongolab hosted database?
Your command line should look like this:
mongoimport -d <databasename> -c <collectionname> --type csv --file <filelocation/file.csv> --host <hostdir example:ds011291.mlab.com> --port <portnumber example:11111> -u <username> -p <password> --headerline
The host direction and the port number it gived by mlab when you create the database.
Example:
ds000000.mlab.com:000000/databaseName

How to export collection to CSV in MongoDB?

How do you export all the records in a MongoDB collection to a .csv file?
mongoexport --host localhost --db dbname --collection name --type=csv > test.csv
This asks me to specify name of the fields I need to export. Can I just export all the fields without specifying the names of fields?
#karoly-horvath has it right. Fields are required for csv.
According to this bug in the MongoDB issue tracker https://jira.mongodb.org/browse/SERVER-4224 you MUST provide the fields when exporting to a csv. The docs are not clear on it. That is the reason for the error.
Try this:
mongoexport --host localhost --db dbname --collection name --csv --out text.csv --fields firstName,middleName,lastName
UPDATE:
This commit: https://github.com/mongodb/mongo-tools/commit/586c00ef09c32c77907bd20d722049ed23065398 fixes the docs for 3.0.0-rc10 and later. It changes
Fields string `long:"fields" short:"f" description:"comma separated list of field names, e.g. -f name,age"`
to
Fields string `long:"fields" short:"f" description:"comma separated list of field names (required for exporting CSV) e.g. -f \"name,age\" "`
VERSION 3.0 AND ABOVE:
You should use --type=csv instead of --csv since it has been deprecated.
More details: https://docs.mongodb.com/manual/reference/program/mongoexport/#export-in-csv-format
Full command:
mongoexport --host localhost --db dbname --collection name --type=csv --out text.csv --fields firstName,middleName,lastName
Also, you are not allowed spaces between comma separated field names.
BAD:
-f firstname, lastname
GOOD:
-f firstname,lastname
mongoexport --help
....
-f [ --fields ] arg comma separated list of field names e.g. -f name,age
--fieldFile arg file with fields names - 1 per line
You have to manually specify it and if you think about it, it makes perfect sense. MongoDB is schemaless; CSV, on the other hand, has a fixed layout for columns. Without knowing what fields are used in different documents it's impossible to output the CSV dump.
If you have a fixed schema perhaps you could retrieve one document, harvest the field names from it with a script and pass it to mongoexport.
If you want, you can export all collections to csv without specifying --fields (will export all fields).
From http://drzon.net/export-mongodb-collections-to-csv-without-specifying-fields/ run this bash script
OIFS=$IFS;
IFS=",";
# fill in your details here
dbname=DBNAME
user=USERNAME
pass=PASSWORD
host=HOSTNAME:PORT
# first get all collections in the database
collections=`mongo "$host/$dbname" -u $user -p $pass --eval "rs.slaveOk();db.getCollectionNames();"`;
collections=`mongo $dbname --eval "rs.slaveOk();db.getCollectionNames();"`;
collectionArray=($collections);
# for each collection
for ((i=0; i<${#collectionArray[#]}; ++i));
do
echo 'exporting collection' ${collectionArray[$i]}
# get comma separated list of keys. do this by peeking into the first document in the collection and get his set of keys
keys=`mongo "$host/$dbname" -u $user -p $pass --eval "rs.slaveOk();var keys = []; for(var key in db.${collectionArray[$i]}.find().sort({_id: -1}).limit(1)[0]) { keys.push(key); }; keys;" --quiet`;
# now use mongoexport with the set of keys to export the collection to csv
mongoexport --host $host -u $user -p $pass -d $dbname -c ${collectionArray[$i]} --fields "$keys" --csv --out $dbname.${collectionArray[$i]}.csv;
done
IFS=$OIFS;
works for me remoting to a docker container with mongo:4.2.6
mongoexport -h mongodb:27017 --authenticationDatabase=admin -u username -p password -d database -c collection -q {"created_date": { "$gte": { "$date": "2020-08-03T00:00:00.000Z" }, "$lt": { "$date": "2020-08-09T23:59:59.999Z" } } } --fields=somefield1,somefield2 --type=csv --out=/archive.csv
Easy export csv or json file With Mongo Compass tool
Mongo Compass As the GUI for MongoDB, MongoDB Compass allows you to make smarter decisions about document structure, querying, indexing, document validation, and more. Commercial subscriptions include technical support for MongoDB Compass.
https://www.mongodb.com/try/download/compass
I could not get mongoexport to do this for me. I found that,to get an exhaustive list of all the fields, you need to loop through the entire collection once. Use this to generate the headers. Then loop through the collection again to populate these headers for each document.
I've written a script to do just this. Converting MongoDB docs to csv irrespective of schema differences between individual documents.
https://github.com/surya-shodan/mongoexportcsv
Also if you want to export inner json fields use dot (. operator).
JSON record:
{
"_id" : "00118685076F2C77",
"value" : {
"userIds" : [
"u1"
],
"deviceId" : "dev"
}
mongoexport command with dot operator (using mongo version 3.4.7):
./mongoexport --host localhost --db myDB --collection myColl
--type=csv --out out.csv --fields value.deviceId,value.userIds
Output csv:
value.deviceId,value.userIds
d1,"[""u1""]"
d2,"[""u2""]"
Note: Make sure you do not export an array. It would corrupt the CSV format like field userIds shown above
Solution for MongoDB Atlas users!
Add the --fields parameter as comma separated field names enclosed in double inverted quotes:
--fields "<FIELD 1>,<FIELD 2>..."
This is complete example:
mongoexport --host Cluster0-shard-0/shard1URL.mongodb.net:27017,shard2URL.mongodb.net:27017,shard3URL.mongodb.net:27017 --ssl --username <USERNAME> --password <PASSWORD> --authenticationDatabase admin --db <DB NAME> --collection <COLLECTION NAME> --type <OUTPUT FILE TYPE> --out <OUTPUT FILE NAME> --fields "<FIELD 1>,<FIELD 2>..."
This working for me Try it
mongoexport --host cluster0-shard-dummy-link.mongodb.net:27017 --db yourdbname --forceTableScan --collection users --type json --out /var/www/html/user.json --authenticationDatabase admin --ssl --username Yourusername --password Yourpassword
Above cmd return whole data of the users collection
if you want filter field then add --fields=email,name
For all those who are stuck with an error.
Let me give you guys a solution with a brief explanation of the same:-
command to connect:-
mongoexport --host your_host --port your_port -u your_username -p your_password --db your_db --collection your_collection --type=csv --out file_name.csv --fields all_the_fields --authenticationDatabase admin
--host --> host of Mongo server
--port --> port of Mongo server
-u --> username
-p --> password
--db --> db from which you want to export
--collection --> collection you want to export
--type --> type of export in my case CSV
--out --> file name where you want to export
--fields --> all the fields you want to export (don't give spaces in between two field name in between commas in case of CSV)
--authenticationDatabase --> database where all your user information is stored
Below command used to export collection to CSV format.
Note: naag is database, employee1_json is a collection.
mongoexport --db naag--collection employee1_json --type csv --out /home/orienit/work/mongodb/employee1_csv_op1