Mongodb export csv in sorted order - mongodb

I'm trying to export an entire MongoDB collection sorted by some of the fields. I'm led to believe that the following should work:
$ mongoexport --csv -d my_db -c my_collection -f field1.subfield,field2.subfield -o d.csv -q '{$query:{},$orderby:{"field1.subfield":1}}'
Unfortunately, this only exports one record in the collection (there are 18478 records) and the data exported is blank. Leaving the $orderby blank, like so,
$ mongoexport --csv -d my_db -c my_collection -f field1.subfield,field2.subfield -o d.csv -q '{$query:{},$orderby:{}}'
, exports the whole collection the way I want, so clearly the orderby clause is wrong. What am I doing wrong?

mongoexport utility does not expect you to sort data.

Related

mongodump 1 single document fails because ObjectId

I'm trying to create a mongo dump for 1 single document inside a mongo collection.
When I do this on windows command line:
mongodump /host:x.x.x.x /port:27017 /username:my_user /password:my_pass -d my_db -o C:\fsdump -c "fs.files" -q '{_id: ObjectId("28ad7bkjia3e927d690385ec")}'
I get this error:
positional arguments not allowed: [ObjectId(28ad7bkjia3e927d690385ec)}']
when I change the id in mongo from ObjectId("28ad7bkjia3e927d690385ec") to "28ad7bkjia3e927d690385ec", and when I dump like this:
mongodump /host:x.x.x.x /port:27017 /username:my_user /password:my_pass -d my_db -o C:\fsdump -c "fs.files" -q '{_id: "28ad7bkjia3e927d690385ec"}'
then it works as expected
so my question is how can I use mongodump and do filtering on specific ObjectId's ?
or is there another way to create an export for a subset of documents in a collection (instead of the entire collection)?
ObjectId("28ad7bkjia3e927d690385ec") is a javascript function call to the ObjectId constructor. This is not valid JSON. "28ad7bkjia3e927d690385ec" is also not a valid ObjectId.
Mongodump uses an extended form of JSON, which had a tag to indicate the field type, so you would specify an ObjectId like this:
-q '{"_id":{"$oid":"5f76b7cc0311bd14f80a3dec"}}'

mongoexport fields from subdocuments to csv

I am trying to export a field from a subdocument with no luck.
Here is my syntax;
mongoexport -d test -c accounts -f account_number,situses.coordinates -o coordinates.csv --type=csv
The output includes the account_number but not the coordinates field from the subdocument.
According to the docs, this is supposed to work.
The following will export the entire situses subdocument, but I only want the one field.
mongoexport -d test -c accounts -f account_number,situses -o coordinates.csv --type=csv
Am I just referencing the subdocument field wrong or something?
I'm running Mongodb 3.0.4
ADDITIONAL INFO
The following syntax worked on an earlier version of Mongodb (2.6.x ?). Notice the subdoc.0.fieldname syntax.
mongoexport -d test -c accounts -f account_number,situses.0.coordinates -o coordinates.csv --csv
It appears support for directly referencing a subdocument has been removed.
There is mistake in your syntax.
From mongo version 3.0.0, mongoexport removed the --type = csv option. Use the --type=csv option to specify CSV format for the output.
You should use :
mongoexport --db tests --collection accounts --type=csv --fields account_number,situses --out coordinates.csv
For nested fields you should use :
mongoexport --db tests --collection accounts --csv --fields 'account_number,situses.0.coordinates' --out /home/vishwas/c1.csv
EDIT for mongo 3.0 with sub documents:
You need to create separate collection with required fields from subdocuments like -
db.test.aggregate({"$unwind":"$situses"},{"$project":{"_id":0,"account_number":1,"siteUsesCo":"$situses.coordinates"}},{"$out" : "forcsv"})
If you want only one field from subdocument then use aggregation like -
db.test.aggregate({"$unwind":"$situses"},{"$limit":1},{"$project":{"_id":0,"account_number":1,"siteUsesCo":"$situses.coordinates"}},{"$out" : "forcsv"})
And then export from forcsv collection like-
mongoexport --db test --collection forcsv --csv --fields 'account_number,siteUsesCo' --out coordinates.csv
And after exporting delete collection forcsv.
And one more solution, where you can configure output in flexible way
mongo host:port/test --quiet query.js -u username -p passw0rd > accounts.csv
and query.js:
db = db.getSiblingDB('test');
db.getCollection('accounts').find({}, {account_number:1, situses:1, _id:0}).forEach(
function(item_data) { print(`${item_data.account_number},${item_data.situses[0].coordinates}`); });
It looks like this is a known bug to be fixed in 3.0.5.
See this; https://jira.mongodb.org/browse/TOOLS-657

How to dump single field for Mongo 2.4?

I'm trying to dump all values of just one field (id) in a particular collection using mongodump. I.e. the same as returned by this query:
db.my_collection.find({},{id:1})
I'm trying
mongodump -d my_database -c my_collection -q -q "{},{id:1}"
When I do this, it dumps all fields.
Another question has suggested
mongodump -d my_database -c my_collection -f id
But it looks like that flag has been removed because
ERROR: unknown option -f
AFAIK mongodump does not support projection. You can use mongoexport:
mongoexport -d my_database -c my_collection --fields id

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