简体   繁体   English

Mongoexport 尝试导出超过 120 天的条目,shell 变量不起作用

[英]Mongoexport trying to export entries older than 120 days, shell variable not working

I have made a shell script and I am trying to export entries older than 1 day from my db.我制作了一个 shell 脚本,我正在尝试从我的数据库中导出超过 1 天的条目。 My DB has a field named "problemDate" which contains dates in string format in yyyy-mm-dd format such as (2021-03-22)我的数据库有一个名为“problemDate”的字段,其中包含 yyyy-mm-dd 格式的字符串格式的日期,例如 (2021-03-22)

mongoexport --host localhost --db pagesuccess --collection problem -q='{ "problemDate": { "$lte" : "2021-03-22"}} ' --type=csv --out text2.csv --fields date,problemDetails,problem,tags,url,language,institution,section,theme
  connected to: mongodb://localhost/
  exported 8 records
--------------------------------------------------------------------------------------------------------------------
older1Day=$(gdate --date="1 days ago" +%F)
mongoexport --host localhost --db pagesuccess --collection problem -q='{ "problemDate": { "$lte" : "$older1Day"}} ' --type=csv --out text2.csv --fields date,problemDetails,problem,tags,url,language,institution,section,theme
  connected to: mongodb://localhost/
  exported 0 records

I'm really not sure about why the second does not work but the first works.我真的不确定为什么第二个不起作用,但第一个起作用。 The only difference is I am using a variable for the date in the second command.唯一的区别是我在第二个命令中使用了日期变量。 I'm really curious as to why the second one does not work.我真的很好奇为什么第二个不起作用。

The variables won't be expanded without placing them in double as opposed to single quotes.如果不将它们放在双引号而不是单引号中,则不会扩展变量。 The command therefore needs to be re-written as:因此,该命令需要重写为:

mongoexport --host localhost --db pagesuccess --collection problem -q="{ \"problemDate\": { \"$lte\" : \"$older1Day\"}}" --type=csv --out text2.csv --fields date,problemDetails,problem,tags,url,language,institution,section,theme

the problem was because I was using single quotes.问题是因为我使用的是单引号。 In Bash you can't interpolate variables in single quotes like you can with double quotes.在 Bash 中,您不能像使用双引号那样在单引号中插入变量。

This worked well:这很好用:

older120Days=$(gdate --date="1 days ago" +%F)
mongoexport --host localhost --db pagesuccess --collection problem -q='{ "problemDate": { "$lte" : "'"$older120Days"'"}} ' --type=csv --out text2.csv --fields date,problemDetails,problem,tags,url,language,institution,section,theme

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM