zoukankan      html  css  js  c++  java
  • MongoDB中的数据导出为excel CSV 文件

     

    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 date

    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()

    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",
    }
    
  • 相关阅读:
    api1
    录像时调用MediaRecorder的start()时发生start failed: -19错误
    继承AppCompatActivity的Activity隐藏标题栏
    Android 6.0 运行时权限处理完全解析
    Android开发用过的十大框架
    Lite Your Android English
    2015最流行的Android组件、工具、框架大全
    C#调用C++函数入口点的问题 z
    C#调用C++的DLL函数另一则(delegate) z
    C#调用C++编写的DLL函数, 以及各种类型的参数传递 z
  • 原文地址:https://www.cnblogs.com/williamjie/p/9152108.html
Copyright © 2011-2022 走看看