如何将值传递给mongoDB命令下面的



是bash脚本,我想将for循环中的‘a’值传递给mongoDB命令,但我得到了$a未定义错误。如何将值传递给mongo命令。

{"account_id":$a}是下面命令的一部分。

#!/usr/bin/bash
for a in `cat /tmp/account.txt`;
do
mongo --authenticationDatabase admin -u 'xxxxxx' -p 'xxxxxxxxx' --quiet --eval 'db.executions.find({ $and : [  { "account_id" : $a }, { "status" : "running" }, { "created_at" : { "$gt" : ISODate("2022-09-01T00:00:00.000Z") } }, {  "created_at" : { "$lt" : ISODate("2022-10-01T00:00:00.000Z") } } ] } ,{"account_id":1,"created_at":1} ).count()' selfservice_production;
done

account.txt

35331
35332
35333
35334
35335
35336
35337
35338
35339
35340

需要在单引号中添加变量。它对我有用。

{"account_id":"$a'"}是我更改的内容。

#!/usr/bin/bash
for a in `cat /tmp/account.txt`
do
mongo --authenticationDatabase admin -u "xxxx" -p "xxxxxxxxxx" --quiet selfservice_production --eval 'db.executions.find({ $and : [  { "account_id" : "'$a'" }, { "status" : "running" }, { "created_at" : { "$gt" : ISODate("2022-08-01T00:00:00.000Z") } }, {  "created_at" : { "$lt" : ISODate("2022-09-01T00:00:00.000Z") } } ] } ,{"account_id":1,"created_at":1} ).count()' 
done

参考:在mongodb 中使用带有--eval的变量

最新更新