从sed命令使用Array时,MongoDB $ in Operator Limit

时间:2017-08-16 21:46:10

标签: arrays bash mongodb sed

我正在尝试运行大量数据。问题是,当我在数组中传递100个对象时,它工作得非常好,但是当我保持150或更多时,它开始失败。

示例: -

DBQuery.shellBatchSize = 100000 ; 
permissibleCars = [  "C:1456797:665","C:146:5722","C:145:57805","C:146:6070","C:14:60908"]
db.getCollection('contracts').aggregate([
{$match:
  {         "methods.name": "image",
            "methods.status": "ACTIVE",   
            container: {"$in": permissibleCars},
            Class : "Download"

} },            
 {"$group" : {_id:"$container", count:{$sum:1}}}],
 { allowDiskUse: true}
);

这将完全正常,直到允许汽车的极限值为100,但是当它超过150左右时,它会随机开始失败并出现以下错误。

2017-08-16T21:30:35.101+0000 E QUERY    [thread1] SyntaxError: unterminated string literal @(shell):1:4091
2017-08-16T21:30:35.132+0000 E QUERY    [thread1] SyntaxError: missing ; before statement @(shell):1:6
2017-08-16T21:30:35.162+0000 E QUERY    [thread1] SyntaxError: missing ; before statement @(shell):1:2
2017-08-16T21:30:35.193+0000 E QUERY    [thread1] ReferenceError: permissibleCars is not defined :

现在因为它运行正常,它不能是语法问题。

无论如何要修复这个问题,以便我可以传递更多的变量。我通过shell运行它。

 for((i=0; i < ${#arr[@]}; i+=batchsize))
   do
    set display=lastline
    IFS=,
    part=( "${arr[@]:i:batchsize}" )
    { echo "DBQuery.shellBatchSize = $contracts_count ; "; cat query/container_count_tmp.js; } > query/container_count.js
    sed -i  "2i permissibleCars = [  ${part[*]} ]"  query/container_count.js
    mongo mngdb-test-02:27068/test_db -u test_user -p test123 < query/container_count.js >> output/container_count.txt
 done 

数组推断: -

distinct_array=`sed ':a;N;$!ba;s/\n/ /g' output/userdistinct.txt`
declare -a arr=($distinct_array)
echo " Total Number of Distinct Ids Stored in Array ${#arr[@]}"
batchsize=150

任何帮助都将受到高度赞赏。 注意: - 我检查了页面mongodb $in limit没有太多信息。

上传了样本数据以进行测试并复制问题。 https://drive.google.com/file/d/0ByHEfbo541jIYlJhSGJIdElCODQ/view?usp=sharing

此致

1 个答案:

答案 0 :(得分:0)

这不是MongoDB限制,但如果您使用POSIX标准sed实现,则字节长度限制为8192字节。这可以解释为什么由于数组字符串被截断而导致语法错误。

https://www.gnu.org/software/sed/manual/html_node/Limitations.html

要获得解决方法,请使用perl而不是sed:

perl -ni -e&#34; print; print&#39; permissibleCars = [$ {part [*]}]&#39;如果$。 == 2&#34;查询/ container_count.js