没有_id字段的mongoexport

时间:2012-10-19 14:13:59

标签: json mongodb

我正在使用mongoexport将一些数据导出到.json格式的文件中,但是文档具有_id:IDVALUE元组引入的大量开销。

我发现了一个类似的帖子Is there a way to retrieve data from MongoDB without the _id field?,关于如何在从mongo检索数据时省略_id字段,但不能导出。建议使用:.Exclude("_id")。我试图重新命名mongoexport的--query参数以某种方式包含.Exclude("_id")参数,但到目前为止所有尝试都失败了。

请说明这样做的正确方法是什么,还是应该恢复使用某些出口后技术?

由于

12 个答案:

答案 0 :(得分:11)

似乎无法使用mongoexport排除某个字段(例如_id)。

以下是适用于中等规模数据库的替代方案:

mongo myserver/mydb --quiet --eval "db.mycoll.find({}, {_id:0}).forEach(printjson);" > out.txt

在一个大型数据库(数百万条记录)上,它可能需要一段时间,运行它会影响人们尝试在系统上执行的其他操作:

答案 1 :(得分:6)

我知道您指定要导出JSON,但如果您可以替换CSV数据,则原生mongo导出将起作用,并且将比上述解决方案快得多

mongoexport --db <dbName> --collection <collectionName> --csv --fields "<fieldOne>,<fieldTwo>,<fieldThree>" > mongoex.csv

答案 2 :(得分:6)

这有效:

{{1}}

答案 3 :(得分:4)

我应用了quux00的解决方案,但forEach(printjson)在输出中打印了MongoDB Extended JSON符号(例如"last_update" : NumberLong("1384715001000")

最好使用以下代码:

db.mycoll.find({}, {_id:0}).forEach(function (doc) {

    print( JSON.stringify(doc) );
});

答案 4 :(得分:4)

mongoexport似乎没有这样的选择。

_id剥离mongoexport --db mydb --collection mycoll -f name,age | R 'omit ["_id"]' 看起来像是:

<script type = "text/javascript">
        function GetSelectedRow(UserLink) {
            var row = UserLink.parentNode.parentNode;
            var rowIndex = row.rowIndex - 1;
            var Userid = row.cells[0].innerHTML;
            alert("RowIndex: " + rowIndex + " Userid : " + Userid + ");
            return false;
        }
    </script>

答案 5 :(得分:2)

defaultConfig { multiDexEnabled true //this is the line you need to enter applicationId "xxxxxx" minSdkVersion xxxxx targetSdkVersion xxxxx versionCode xx versionName "xx" } 的输出放入mongoexport并在其中删除jq字段。

_id

更新:添加link to jq

答案 6 :(得分:1)

mongo <server>/<database> --quiet --eval "db.<collection>.find({}, {_id:0,<field>:1}).forEach(printjson);" > out.txt

如果您有一些查询要执行更改""'',请在find中使用"" find("age":13)写下您的条件。

答案 7 :(得分:0)

排除子文档信息(如“_id”)的最简单方法是将其导出为csv,然后使用工具将csv转换为json。

答案 8 :(得分:0)

mongoexport 无法省略“_id”

sed 是一个强大的命令:

mongoexport --db mydb --collection mycoll -f name,age | sed '/"_id":/s/"_id":[^,]*,//'

原始答案来自Exclude _id field using MongoExport command

答案 9 :(得分:0)

只需在mongoexport命令中使用-type = csv 选项。

mongoexport --db=<db_name> --collection=<collection_name> --type=csv --field=<fields> --out=<Outfilename>.csv

对于MongoDb 3.4版,您也可以在mongoexport命令中使用-noHeaderLine 选项在csv导出中也排除字段标题。

有关详细信息:https://docs.mongodb.com/manual/reference/program/mongoexport/

答案 10 :(得分:0)

导出到文件中,就我而言,使用正则表达式替换空值

"_id": "f5dc48e1-ed04-4ef9-943b-b1194a088b95"

我使用了"_id": "(\w|-)*"

答案 11 :(得分:-6)

您是否尝试使用--fields标记指定字段?所有未提及的字段都会从export中排除。

为了便于维护,您还可以将字段写入单独的文件并使用--fieldFile

相关问题