MongoDB中的資料匯出為excel CSV 檔案

來源:互聯網
上載者:User

標籤:nta   apple   cto   bubuko   ESS   one   app   field   oda   

1、開啟命令列,進入我們所安裝的mongodb路徑下的bin檔案夾

2、我們採用bin檔案夾下的mongoexport方法進行匯出, 

1 mongoexport -d myDB -c user -f _id,name,password,adress --csv -o ./user.csv
  1. -d  標示 資料庫  
  2.  -c   標示  資料表  
  3.  -f   需要提取的field用逗號分隔  
  4.  -o  輸出路徑  

 

 

ongoexport query with using date30 Mar 2014

Sometimes we might want to export only a specific part of our collection with query support of mongoexport.

Suppose this is our notebook collection, and each 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",        "date" : ISODate("2014-03-05T22:00:00Z"),        "price" : 1000,        "brand" : "Ultrabook",}

The original way of 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 that we have to convert each of 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 will probably have an error like;

ERROR: too many positional options

There are two common ways to convert ISODates into Date objects which are;

    • 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 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 will have a json file (example.json) in our current directory which includes only one document which is;

{        "_id" : ObjectId("531ce460000000019b9643bd"),        "company" : "Apple",        "date" : ISODate("2014-03-07T22:00:00Z"),        "price" : 2250,        "brand" : "MacbookPro",}
生產環境匯出命令:   

MongoDB中的資料匯出為excel CSV 檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.