【问题标题】:mongoexport exclude documentsmongoexport 排除文档
【发布时间】:2016-04-01 05:14:15
【问题描述】:

我有一个包含以下文档的 MongoDB 集合。有些文档有 1 个字段,有些有 2 个。我有兴趣仅将具有 2 个字段的文档导出到 CSV 文件。我可以使用什么查询来排除具有 1 个字段的那些?

[
{
    "id" : 1,
},

{
    "id" : 2,
},

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

我的 mongoexport 命令是:mongoexport --username x --password x --host x --db mydb --collection mycol --type=csv --fields id,productid --out "c:\myfile.csv"

【问题讨论】:

标签: mongodb csv mongodb-query mongoexport


【解决方案1】:

试试这个我添加了查询的地方

mongoexport --username x --password x --host x --db mydb --collection mycol --type=csv --fields id,productid --query '{ "$and": [ { "id": {"$exists":true}},{"productId":{"$exists":true}}] }' --out "c:\myfile.csv"

或者是这样的

mongoexport --username x --password x --host x --db mydb --collection mycol --type=csv --fields id,productid --query '{ "id": {"$exists":true},"productId":{"$exists":true} }' --out "c:\myfile.csv"

【讨论】:

  • Suresh,我尝试了这两种方法,但出现错误“json: cannot unmarshal string into Go value of type map[string]interface{}”
  • 在查询字段中使用双引号,例如 { "id": {"$exists":true},"productId":{"$exists":true} }
  • Suresh,您使用双引号的建议确实有效。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-21
  • 1970-01-01
  • 1970-01-01
  • 2022-12-05
  • 2018-06-22
  • 2010-11-28
相关资源
最近更新 更多