1. Open the command line and go to the Bin folder under the MongoDB path we installed
2, we use the Bin folder Mongoexport method to export,
1 |
mongoexport -d myDB -c user -f _id,name,password,adress --csv -o ./user.csv |
- -D Flag Database
- -C Marking Data sheet
- -F The field to be extracted is separated by commas
- -O Output path
Ongoexport query with using dateApril
Sometimes we might want to export is a specific part of our collection with query support of Mongoexport.
Suppose this was our notebook collection, and the document refers to a notebook with their production date.
{"_ID":ObjectId("531CE460000000019B9643BC"),"Company":"Samsung","Date":Isodate("2014-03-09T22:00:00Z"),"Price":2000,"Brand":"Ultrabook",}{"_ID":ObjectId("531ce460000000019b9643ba"),"Company":"Sony","Date":Isodate("2014-03-08T22:00:00Z"),"Price":1500,"Brand":"Vaio",}{"_ID":ObjectId("531CE460000000019B9643BD"),"Company":"Apple","Date":Isodate("2014-03-07T22:00:00Z"),"Price":2250,"Brand":"Macbookpro",}{"_ID":ObjectId("531ce460000000019b9643be"),"Company":"Apple","Date":Isodate("2014-03-06T22:00:00Z"),"Price":1200,"Brand":"Macbookair",}{"_ID":ObjectId( "531CE460000000019B9643BF" ) , "Company" : "Samsung" Span class= "token punctuation", : isodate" 2014-03-05t22:00:00z " "price" : 1000" Brand "" Ultrabook " /span>
The original-mongoexport is defined by
Mongoexport--db <database>--collection <collection>--query <json query>--out <file>
The major problem is we can not use isodate ("") objects to represent dates within a query, so, we had to con Vert each of the them object into a Date object.
for instance; If we try to find the notebooks produced between 2014-03-09t22:00:00z and 2014-03-07t22:00:00z by Apple with the given query;
Mongoexport--db test--collection Notebooks--query ' {company: ' Apple ', date: {$lt: Isodate ("2014-03-09t22:00:00z" ), $gte: Isodate ("2014-03-07t22:00:00z")}} '--out Example.json
We'll probably have a error like;
Error:too many positional options
There is common ways to convert isodates into Date objects which is;
- We can use a simple JavaScript in MONGO shell like;
var a = isodate (' 2014-03-10t22:00:00z '); A.gettime ()
- We can convert the given date into milliseconds from the link isodate to milliseconds
Now we have the correct Date times to use them in our query.
Mongoexport--db test--collection Notebooks--query "{company:" Apple, Date: {$lt: new date (1394402400000), $gte: New Date (1394229600000)}} "--out Example.json
Finally, we'll have the a JSON file (Example.json) in your current directory which includes only one document which is;
{"_ID":ObjectId( "531ce460000000019b9643bd" ) , "Company" : "Apple" : isodate (" 2014-03-07t22:00:00z " "price" : 2250" Brand "" Macbookpro " , }
Production Environment Export command:
Data in MongoDB is exported as an Excel CSV file