Why does mongoexport query with $exist fail? - mongodb

I am trying to mongoexport (Version 2.6) MongoDB data into csv format using the command as follows
mongoexport --port 27017 -d test -q "{userId:{$exists:true} , name:'John'}"-c user_datas -f userId --csv -o /myOutFile.csv
and i got this error message:
assertion: 16619 code FailedToParse: FailedToParse: First character in field must be [A-Za-z$_]: offset:9 of:{userId:{true},name:John}
according to error message that something happened on '$exists' that caused the error .
whats wrong with my command?

You need to invert the quotes:
'{userId: {$exists: true} , name: "John"}'
Working command:
mongoexport \
--port 27017 \
-d test \
-q '{userId: {$exists: true} , name: "John"}' \
-c user_datas \
-f userId \
--csv \
-o /myOutFile.csv

Related

mongoexport works on local but not on remote server

When I run this code to export my local database, it works well and a file is successfully generated:
mongoexport \
--host="localhost:27017" \
-d springDb \
-c posts \
--out social.json
But when I try to access my remote server with the similar command here:
mongoexport \
--host="mongodb+srv://clustxxx.xxx.mongodb.net/xxxx" \
-d socialDeveloper \
-c posts \
--out social.json
I get this error:
error parsing command line options: error parsing URI from mongodb:///clustxxx.xxx.mongodb.net/xxxx:27017/?replicaSet=mongodb+srv:: error parsing uri: must have at least 1 host
What am I doing wrong please?

Mongoexport shell script - Error in query

I wrote a shell script to create a data dump of records updated yesterday using mongoexport command.
yesterday=$(date -d 'yesterday 00:00:00' '+%s'000)
today=$(date -d 'today 00:00:00' '+%s'000)
query="'{\"updated_at\":{\$gte:new Date(${yesterday}),\$lte:new Date(${today})}}'"
echo ${query}
mongoexport -h $HOST -d $DOC -c $COL_NAME -u $USER -p $PWD -q ${query} -o $fileName
After adding query, when I run the shell script I get below error in console
'{"updated_at":{$gte:new Date(1484287200000),$lte:new Date(1484373600000)}}'
too many positional arguments: [Date(1484287200000),$lte:new Date(1484373600000)}}']
try 'mongoexport --help' for more information
When I run this query in command line it works properly. Can someone pls let me know why is this error when ran in shell script?
This works in command line.
$mongoexport -h <<HOST>> -d <<DOC>> -c <<COL> -u <<UN>> -p <<PWD>> -q '{"updated_at":{"$gte":new Date(1484287200000),"$lte":new Date(1484373600000)}}'
There is a rule of thumb in bash: when you use a variable, always surround it with double quotes. There are exceptions, but they are rare.
mongoexport -h "$HOST" -d "$DOC" -c "$COL_NAME" -u "$USER" -p "$PWD" -q "${query}" -o "$fileName"
This code worked for me
yesterday=$(date -d 'yesterday 00:00:00' '+%s'000)
today=$(date -d 'today 00:00:00' '+%s'000)
query1="{\"transactionDate\":{\$gte: new Date(${yesterday}),\$lte: new Date(${today})}}"
echo $yesterday
echo $today
mongoexport -d databasename-c collectionname --host yourip --port 27017 -p password -u username-q "${query1}" --type=csv --fields=transactionDate,amount > test5.csv

Return MongoDB query result to shell

I use the mongoimport command in shell to load a json file in MongoDB.
mongoimport \
--host ${MONGO_HOST}:${MONGO_PORT} \
--db ${MONGO_DB} \
--type json \
--collection ${COLLECTION} \
--file ${DATA_IN_PATH}/${FILENAME}.json \
--upsert \
--upsertFields ${UPSERT_FIELDS}
I would like to count the number of documents in my collection, before and after the load and get it in a shell variable
I tried Using the --eval command and put the result into a variable:
CollCount=$(mongo \
${MONGO_HOST}:${MONGO_PORT}/${MONGO_DB} \
--eval "db.getCollection('${COLLECTION}').count({})")
but my var CollCount contains:
MongoDB shell version: 2.6.9 connecting to: localhost:27017/mydb 12236
(The value 12236 is correct.)
Is it a better way to do this?

mongo import an empty array get error?

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

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