13

我有这个查询,但我收到一个语法错误:意外标识符

mongoexport --db ium --collection events \
  --query 'db.events.find({'created_at' : {
      $gte: ISODate("2016-03-01T00:00:00.001Z"),
      $lte: ISODate("2016-03-29T23:59:59:59.000Z")
    }, 
    "name" : "UPDATE_SUCCESS"})' \
 --out guille1_test.json

有什么问题?

4

3 回答 3

21

You need to use "extended json" in queries with mongoexport. So the way to specify "dates" is with $date instead. And the --query is just the "query string" in JSON format. Not the whole command entered into the shell:

mongoexport --db ium --collection events \
  --query '{ 
    "created_at": { 
      "$gte": { "$date": "2016-03-01T00:00:00.001Z" },
      "$lte": { "$date": "2016-03-29T23:59:59.000Z" }
    },
    "name": "UPDATE_SUCCESS"
  }' \
  --out guile1_test.json

Note also the corrected date string in the $lte argument and of course the "quoting" use of '' around the body of the JSON argument and "" around the internal expressions and values. It s important that these types of quotes are different, as well as "shell arguments" should have their "outer" quotes as '', otherwise the "shell" tries to evaluate the expression contained.

于 2016-03-31T00:49:44.793 回答
3

另一种可行的解决方案是使用MongoDB 手册new Date()中描述的构造函数。这将导致更小的查询主体占用空间,如下所示:

    mongoexport --db ium --collection events \
  --query '{ 
    "created_at": { 
      "$gte": new Date("2016-03-01T00:00:00.001Z"),
      "$lte": new Date("2016-03-29T23:59:59.000Z")
    },
    "name": "UPDATE_SUCCESS"
  }' \
  --out guile1_test.json

这种方法对我来说是开箱即用的,而所有其他选择都失败了。这里有一篇相关文章描述了这种方法。

于 2018-05-10T13:47:15.940 回答
1

实现它的最佳方法如下。因为 new Date 和 IOSDate 对于这个命令来说是无效的文字。

对于远程主机

mongoexport --host {{host}} --username {{username}} --password {{passord}} --authenticationDatabase admin --db {{Database}} --collection {{collection Name}} --query '{ "date" : { "$gt" : {"$date":"2019-10-31T00:00:00.000Z"}  } }'  --type json --out {{path of directory where you would want to export file.}}

对于本地主机

 mongoexport  --db {{Database}} --collection {{collection Name}} --query '{ "date" : { "$gt" : {"$date":"2019-10-31T00:00:00.000Z"}  } }'  --type json --out {{path of directory where you would want to export file.}}
于 2019-11-20T06:18:48.177 回答