Data in MongoDB is exported as an Excel CSV file

Source: Internet
Author: User
Tags mongodb mongo shell

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
    1. -D Flag Database
    2. -C Marking Data sheet
    3. -F The field to be extracted is separated by commas
    4. -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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.