mongo import an empty array get error? - mongodb

this is about mongodb.
as we know,mongo offer import and export through array by argument --jsonArray,but there is problem:
I have a empty collection,and I execute
mongoexport -d test -c myCollection -o --jsonArray mycol.json
I got a json file:[].
And then I execute
mongoimport -d test -c myCollection --jsonArray mycol.json
I got an error:
Failed: error processing document #1: invalid character ']' looking for beginning of value.
I mean,it's mongo himself export the json,but he cannot recognise it.It's some weird.

You can consider removing the --jsonArray flag while running the first command:
mongoexport -d test -c myCollection -o mycol.json
If the collection is empty, it will output an empty file which will should work when importing with:
mongoimport -d test -c myCollection mycol.json

Related

MongoDB: add a date using mongoimport with --columnsHaveTypes --headerline

I have a test.tsv TSV file that has a header looking like this
sample.string() organism.string() capture.string() sex.string()
and I am using the following command to import my file into mongodb
mongoimport --quiet -d somedb --collection=somecollection --file=test.tsv --type tsv --mode upsert --upsertFields sample --columnsHaveTypes --headerline
I wish to add the date following mongodb format at the moment of my file import.
I found a way to do it while in mongodb but I can't seem to find anything about mongoimport.
Is there a command (or a workaround) that looks like the following and allows us to add the date of the file content ?
mongoimport --quiet -d somedb --collection=somecollection --file=test.tsv --type tsv --mode upsert --upsertFields sample --columnsHaveTypes --headerline --addDate
Thanks in advance.
I found a workaround for this:
First, get the date from bash with some formatting:
date=$(date -u +"%d-%m-%Y %T")
Now, add this variable to all the lines in file.tsv like so:
awk -v date="$date" '{print $0 "\t" date}' file.tsv
Then, we can change the header by adding the date format type
$ sed -i '1s/.*/Sample.string()\tOrganism.string()\tCapture.string()\tSex.string()\tDate.date(02-01-2006 15:04:05)/' file.tsv
$ head file.tsv
Sample.string() Organism.string() Capture.string() Sex.string() Date.date(02-01-2006 15:04:05)
Then, we can import with mongoimport:
$ mongoimport -d somedb --collection=somecollection --file=file.tsv --type tsv --mode upsert --upsertFields Sample --columnsHaveTypes --headerline
2019-09-30T11:23:37.934+0200 connected to: xxxx
2019-09-30T11:23:37.940+0200 imported 1 document

Best practice mongodump backing up 250GB database

I'm tring another aproach. Full dump and the daily dump of new data using:
oid=$(mongo --quiet --eval 'ObjectId.fromDate(ISODate("2017-03-29 00:10:20"))')
mongodump -q "{_id:{$gt:$oid}}" -d dbname --collection name_data
But I'm getting:
Failed: error parsing query as json: invalid character ':' looking for beginning of object key string
The first $ needs to be escaped:
mongodump -q "{_id:{\$gt:$oid}}" -d dbname --collection name_data

How to import CSV file to MongoDB on Windows?

The mongoshell shows the result of the mongoimport command while trying to import a CSV file into MongoDB:
I wanted to import a csv file into MongoDB. So I have used the command:
mongoimport -d dbname -c collectionname --type csv --file filename.csv --headerline
after executing this command I constantly got an error of missing ; before statement. I stored the file in the path mongo\bin only. Is the command itself wrong or should I save the file any other location than I have saved it now? Can anyone give a way to fix this command?
mongoimport is a binary that runs from OS shell and not the mongo shell.
So
C:\User\Home>mongoimport -d dbname -c collectionname --type csv --file filename.csv --headerline
is correct while
mongo> mongoimport -d dbname -c collectionname --type csv --file filename.csv --headerline
is not.

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

How to get mongo command results in to a flat file

How do I export the results of a MongoDB command to a flat file
For example, If I am to get db.collectionname.find() into a flat file.
I tried db.collectionname.find() >> "test.txt" doesnt seem to work.
you can try the following from the command line
mongo 127.0.0.1/db --eval "var c = db.collection.find(); while(c.hasNext()) {printjson(c.next())}" >> test.txt
assuming you have a database called 'db' running on localhost and a collection called 'collection' this will export all records into a file called test.txt
If you have a longer script that you want to execute you can also create a script.js file
and just use
mongo 127.0.0.1/db script.js >> test.txt
I hope this helps
I know of no way to do that from the mongo shell directly, but you can get mongoexport to execute queries and send the results to a file with the -q and -o options:
mongoexport -h mongo.dev.priv -d models -c profiles -q '{ $query : { _id : "MRD461000" } }' -o MRD_Series1.json
The above hits queries the profiles collection in the models database grabbing the JSON document for _id = "MRD641000". Works for me.
Use this
mongo db_name --username user --password password < query1.js >> result.txt
Try this - returns a json file with the data of the query, you can change .json for .txt and other.
mongoexport --db products --collection clicks --query '{"createdInt":{$gte:20190101}, "clientId":"123", "country":"ES"}' --out clicks-2019.json
Having missed the db needing to be the actual db in Peshkira's answer, here is a general syntax for a one liner in shell (assuming no password):
mongo <host>:<db name> --eval "var x = <db name>.<collection name>.<query>; while(x.hasNext()) { printjson( x.next() ) }" >> out.txt
I tested it both on my mac and Google cloud Ubuntu 15 with Mongo 3+.
Install MongoDB Compass, then it will have a tool to export query result to Json/CSV files.
mongoexport --host 127.0.0.1 --port 27017 --username youruser -p yourpass \
-d yourDatabaseName -c collectionName --type csv \
--fields field1,field2 -q '{"field1" : 1495730914381}' \
--out report.csv
mongoexport --db db_name --collection collection_name --csv --out file_name.csv -f field1,field2, field3