Shell Mongo查询使用$ nin返回0个结果

时间:2018-10-17 09:49:48

标签: mongodb bash shell

我编写了一个脚本以连接到MongoDB数据库以检索信息,并通过另一个基础中的curl请求将其插入。完成后,脚本会将插入的ID写入.txt文件中。当我再次运行脚本时,.txt文件被加载以仅导入不在.txt文件中的ID

但是,当我尝试设置要加载的$nin参数时,Mongo不会显示结果,仅显示.txt文件中没有的行。

下面是我的测试脚本:

#!/bin/bash

filter=$(<personKey.txt)
filter="$(echo $filter|sed -e 's/ /'\'','\''/g')"
filter="$(echo $filter|sed -e s/^/\'/ -e s/$/\',/)"
filter=${filter%?}
echo $filter

echo "db.getCollection('persons').find({'documents.document.personKey': {'\$nin':[${filter}]}},{'documents.document.personKey':1,'documents.document.watchlistKey':1,'documents.document.first_name':1})"

QueryResult=`mongo $IP_MONGO:$PORT_MONGO/MyBase -u "username" -p "Password" --quiet --eval "db.getCollection('person').find({{'documents.document.personKey': {'\$nin':[${filter}]}},{'documents.document.personKey':1,'documents.document.watchlistKey':1,'documents.document.first_name':1})"`

echo ${QueryResult}

0 个答案:

没有答案
相关问题