mongodb 如何使用 mongodump 转储匹配特定日期范围的记录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6996999/
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 12:12:01  来源:igfitidea点击:

How can I use mongodump to dump out records matching a specific date range?

mongodb

提问by Simon Willison

I'm trying to use the mongodump command to dump out a bunch of records created on a specific date. The records include a "ts" field which is a MongoDB Date() object.

我正在尝试使用 mongodump 命令转储在特定日期创建的一堆记录。这些记录包括一个“ts”字段,它是一个 MongoDB Date() 对象。

mongodump takes a -q argument which can be used to run a query to select the records to be included in the dump. Unfortunately, the -q argument needs to be provided in JSON, and it's not clear how to express a "less-than-this-date, more-than-this-date" query in pure JSON (normally such queries would use a 'new Date()' constructor)"

mongodump 采用 -q 参数,可用于运行查询以选择要包含在转储中的记录。不幸的是,需要在 JSON 中提供 -q 参数,并且不清楚如何在纯 JSON 中表达“小于此日期,大于此日期”的查询(通常此类查询将使用 ' new Date()' 构造函数)"

Any tips? I've tried using the {$date: unix-timestamp-in-milliseconds} format but it's not working for me.

有小费吗?我试过使用 {$date: unix-timestamp-in-milliseconds} 格式,但它对我不起作用。

回答by Simon Willison

I solved it - the magic incantation I was looking for is:

我解决了它 - 我正在寻找的魔法咒语是:

mongodump --query "{\"ts\":{\"$gt\":{\"$date\":`date -d 2011-08-10 +%s`000},\"$lte\":{\"$date\":`date -d 2011-08-11 +%s`000}}}"

回答by ericsoco

A more human-readable version than @SimonWillison's escaped version:

比@SimonWillison 的转义版本更易于阅读的版本:

--query "{ time: { \$gt: new Date(1312959600000), \$lt: new Date(1313046000000) }}"

--query "{ time: { \$gt: new Date(1312959600000), \$lt: new Date(1313046000000) }}"

(Note the dollarsigns still need to be escaped.)

(注意美元符号仍然需要转义。)

I got the millisecond timestamps by creating dates in the shell, e.g.:

我通过在 shell 中创建日期来获得毫秒时间戳,例如:

> var targetDateStart = new Date(2011, 7, 10);
> var targetDateEnd = new Date(2011, 7, 11);
> targetDateStart.getTime();
1312959600000
> targetDateEnd.getTime();
1313046000000

回答by diggzhang

In MongoDB 3.2, we can use --queryFileoption with mongodump.

在 MongoDB 3.2 中,我们可以将--queryFileoption 与 mongodump 一起使用。

first of all, create a json file:

首先,创建一个json文件:

//query.json
{"serverTime": {"$gte": ISODate("2016-01-30T16:00:00.000Z"), "$lt": ISODate("2016-01-31T16:00:00.000Z")}}

next,use mongodump:

接下来,使用 mongodump:

mongodump --db <dbName> --collection <collectionName> --queryFile query.json

simple and clear.

简单明了。

回答by ifyouseewendy

Edit: fixed typos

编辑:修正错别字

Add an update:

添加更新:

  1. mongodump --querydoesn't support IsoDate, but accepts Datein milliseconds form.

  2. As datecommand behaves different in OS X, date -d 2011-08-10 +%sdoes not work for me. If you've run into the same issue, try to read the manual or use this:

    • Get current time in seconds:

      date -j -f "%a %b %d %T %Z %Y" "`date`" "+%s"
      
    • Get specific time in seconds:

      date -j -f "%Y-%m-%d %H:%M:%S" "2014-01-01 00:00:00"  "+%s"
      
  3. Use the single quote version to avoid escaping.

    mongodump --query '{updated_at: { $gte: Date(1403280000000) } }'
    
  1. mongodump --query不支持IsoDate,但Date以毫秒形式接受。

  2. 由于date命令在 OS X 中的行为不同,date -d 2011-08-10 +%s对我不起作用。如果您遇到同样的问题,请尝试阅读手册或使用以下命令:

    • 以秒为单位获取当前时间:

      date -j -f "%a %b %d %T %Z %Y" "`date`" "+%s"
      
    • 以秒为单位获取特定时间:

      date -j -f "%Y-%m-%d %H:%M:%S" "2014-01-01 00:00:00"  "+%s"
      
  3. 使用单引号版本避免转义。

    mongodump --query '{updated_at: { $gte: Date(1403280000000) } }'
    

回答by mcr

use single quotes around the query. I found that ISODate() doesn't work.

在查询周围使用单引号。我发现 ISODate() 不起作用。

mongodump --query  '{"ts":{$gt:{$date:178929000}}}'

回答by Remon van Vliet

This should work, what didn't work about your $date query? :

这应该有效,您的 $date 查询有什么问题?:

mongodump --query  {"ts":{$gt:{$date:178929000}}}

回答by Dmitriy Botov

In my case I queried entries created 14 days ago and end up with this bash script:

就我而言,我查询了 14 天前创建的条目,最终得到了这个 bash 脚本:

#!/bin/bash
date_now=`date +%s%3N`
date_2weeks_ago=$[date_now - 14 * 24 * 60 * 60 * 1000]
query=$(printf '{ createdAt: { $gte: Date(%d) } }' $date_2weeks_ago)
echo $query > query.json
mongodump \
--collection=data \
--queryFile=query.json
rm query.json

mongodump version: r4.0.12

mongodump 版本:r4.0.12