MongoDB mongoexport 查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21458168/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 13:35:49  来源:igfitidea点击:

MongoDB mongoexport query

mongodbmongodb-query

提问by ChrisGeo

I have a collection of the following form in MongoDB. As you can see some documends have two members "id" and "xid" and some have only 1 "id" (aside from the Object _id)

我在 MongoDB 中有以下形式的集合。正如您所看到的,有些文档有两个成员“id”和“xid”,而有些文档只有 1 个“id”(对象 _id 除外)

[
    {
        "id" : 1,
    },

    {
        "id" : 2,
    },

    {
       "id" : 3
       "xid": 300
    }
]

I want to create a mongoexport statement that exports to a csv only documents with id's AND xid's with the value of xid > 0

我想创建一个 mongoexport 语句,该语句仅将具有 id 和 xid 且 xid > 0 的文档导出到 csv

I currently have the following command:

我目前有以下命令:

mongoexport -h host -u user -p pass --db database --collection collections --csv --fields id,xid --query '{"xid":{"$ne":0}}' --out rec.csv

However this also exports documents that have an id without an xid. So i get something like

但是,这也会导出具有 id 没有 xid 的文档。所以我得到类似的东西

xid, id
12, 3
,4
14, 5
,3
,2
12, 5

etc.

等等。

Is there a way to export documents that only have both id and xid?

有没有办法导出只有 id 和 xid 的文档?

采纳答案by Jinxcat

You can use the following as your query:

您可以使用以下内容作为查询:

{ $and: [
    {xid: {$exists:true}},
    {xid:{$gt:0}}
    ]  
}

$and"performs a logical AND operation on an array of two or more expressions (e.g. , , etc.) and selects the documents that satisfy all the expressions in the array."

$and“对包含两个或多个表达式(例如 、 等)的数组执行逻辑 AND 运算并选择满足数组中所有表达式的文档。

$existschecks if the specific field is present in the document.

$exists检查文档中是否存在特定字段。

Also, as it is being pointed out by Vijay, in your description you mention you want xid > 0. You shouldn't be using $ne (not equal), but $gt (greater than).

另外,正如 Vijay 所指出的,在您的描述中,您提到您想要 xid > 0。您不应该使用 $ne(不等于),而是使用 $gt(大于)。

回答by Darshan J

you can use below commands

您可以使用以下命令

mongoexport -d database_name -c collection_name --csv --query { id:{$exist:true},xid:{$gt:0}}

回答by Vicky

You can change your query to

您可以将查询更改为

--query {id: {$exists : true}, xid: {$gt : 0}}

This above query will return only those documents that have id field and have value of xid > 0. So if there is any document that have only id field not xid, those documents will not be returned.

上面的查询将仅返回具有 id 字段且值 xid > 0 的那些文档。因此,如果存在任何仅具有 id 字段而不是 xid 的文档,则不会返回这些文档。

回答by vijin selvaraj

mongoexport --db db_name --collection collection_name --query "{'Name':'Vijil'}" --out file_name.json

mongoexport --db db_name --collection collection_name --query "{'Name':'Vijil'}" --out file_name.json

回答by arunb2w

From the document given above it seems it is an array of documents, but u are accessing the document without . notation. Seems u are accessing the document wrongly.

从上面给出的文档来看,它似乎是一个文档数组,但是您正在访问该文档而没有 . 符号。似乎您访问文档有误。

Should be something like '{"key.xid":{"$ne":0}}'

应该是这样的 '{"key.xid":{"$ne":0}}'

回答by Sudhanshu Gaur

Remember as of 3.0.6--csvtag is not supported, now in order to use that use --type=csv

记住 as of3.0.6--csv标签不受支持,现在为了使用该使用--type=csv

Below is the query which takes MongoExport on the database db_nameand collection collection_name, Remember to specify field names also(which fields you want to add in your csv file).

下面是在数据库db_name和集合上使用 MongoExport 的查询collection_name,请记住还要指定字段名称(您要在 csv 文件中添加哪些字段)。

sudo mongoexport -d db_name -c collection_name --type=csv --query='{field1:{$gt:2}}' -f "field1,field2" --sort='{field1:-1}' -u 'username' -p 'passwd' --authenticationDatabase=admin --out dir/file.csv