【问题标题】:Mongoexport using operator $in statementMongoexport 使用运算符 $in 语句
【发布时间】:2019-12-03 15:57:26
【问题描述】:

我需要使用 mongodb export 命令提取数据

以下是我在 mongo db 上的数据结构

{
    "_id": "12345",
    "name": "Ebo",
    "salary": NumberInt(350000),
    "age": NumberInt(24)
}
{
    "_id": "67891",
    "name": "Amir",
    "salary": NumberInt(250000),
    "age": NumberInt(24)
}

我尝试在多个字段中使用导出来使用 mongoexport,下面是我要生成的命令。

mongoexport --username abc --password abcde --host mymonggodb:27017 --db school 
--collection employee --type=csv --authenticationDatabase admin -c records 
--query="{'_id': {$in : ['12345','67891','11121314','123456']}}" --fields _id,name,salary,age 
--out /Users/axxxx/abcde/monggoextractdata/monggo_employee_data.csv

但我收到以下错误。

2019-03-07T12:32:23.460+0700    error validating settings: 
query '[123 39 95 105 100 39 58 32 123 32 58 32 91 39 51 52 55 52 82 67 39 44 39 
51 52 48 54 65 71 39 44 39 51 50 56 50 84 70 39 44 39 51 52 49 48 86 51 39 93 
125 125]' is not valid JSON: invalid character ':' 
looking for beginning of object key string 2019-03-07T12:32:23.460+0700 
try 'mongoexport --help' for more information

【问题讨论】:

    标签: mongodb nosql mongoexport


    【解决方案1】:

    MongoDB documentation 中建议的表示法是将您的查询用单引号括起来,并使用双引号指定查询中的字段。使用不同的 shell 时,单引号和双引号的行为可能不同。使用 mongoexport 时,$in(或$gte$lte)之类的运算符也应该用双引号括起来。所以而不是:

    --query="{'_id': {$in : ['12345','67891','11121314','123456']}}"
    

    你应该使用:

    --query='{"_id": {"$in" : ["12345","67891","11121314","123456"]}}'
    

    最后的命令是:

    mongoexport --username abc --password abcde --host mymonggodb:27017 --db school 
    --collection employee --type=csv --authenticationDatabase admin -c records 
    --query='{"_id": {"$in" : ["12345","67891","11121314","123456"]}}'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-30
      相关资源
      最近更新 更多