0

I want to filter some collections in mongodb to export. But the string compare seems incorrect.

$1 in my case is localhost:17017/mydb

shop is one of the collections in mongodb, but $i == 'shop' never succeed.

#!/bin/bash

colls=`mongo $1 --eval 'db.getCollectionNames();' | tail -1`

IFS=',' read -ra ADDR <<< $colls

for i in "${ADDR[@]}"
do
    if [[ $i == 'shop' ]]
    then
        echo $i
    fi
done

Or is there any other methods to export specified collections from mongodb?

1
  • are you trying to match "shop" or "'shop'"? if you need a partial match you can do case "$i" in *shop*)echo $i;;esac ... note the double quotes around $i, in case of odd characters Commented Aug 14, 2013 at 8:03

1 Answer 1

1

Try to echo the values you got and see perhaps how you should actually use the patterns. Also please quote your variables properly. It's also better to use $() over backticks:

#!/bin/bash

colls=$(mongo "$1" --eval 'db.getCollectionNames();' | tail -1)
echo "colls: $colls"

IFS=',' read -ra ADDR <<< "$colls"
echo "colls count: ${#ADDR[@]}"

for i in "${ADDR[@]}"
do
    echo "Trying |$i|."
    if [[ $i == 'shop' ]]
    then
        echo "$i"
    fi
done
Sign up to request clarification or add additional context in comments.

1 Comment

Somehow it prevents the contents of $colls from being re-evaluated with values of IFS before input.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.