Tiller
Tiller

Reputation: 476

Start a command, and then send data to it

I'm developping a little utlity tool which parse some files, extracts a specific information and then store it into a mongodb collection.

It's working right now, but it's doing something like :

#!/bin/bash

find myFolder | while read f
do
    # some things
    ...
    # and then

    echo "db.test.insert({'val': BinData(0, '$data')});" | mongo -quiet --host 127.0.0.1:36427 collectionName
done

But the "problem" here is that I'm opening / closing the mongodb connection everytime.
So here is the question:

Is there a way to start the mongodb connection, and then happen some data into it.

I KNOW that the following code is totally wrong, but it's just to give you an example of the kind of feature I'm looking for :

#!/bin/bash

mongoConn=`mongo 127.0.0.1:36427 collectionName`

find myFolder | while read f
    # ...

    echo "db.test.insert(..);" > mongoConn
done

echo "exit;" > mongoConn

Upvotes: 0

Views: 238

Answers (2)

ceving
ceving

Reputation: 23881

The following assumes, that you can write more than one command to your mongo tool.

Open a pipe to the mongo command as stdout. After that you can write your commands.

exec > >(mongo ...)
find ... | while read ... ; do
  echo "db.test.insert(...);"
done

Upvotes: 2

Idriss Neumann
Idriss Neumann

Reputation: 3838

You could try something like :

#!/bin/bash

deleteTmpFile(){
    [[ -f insert.tmp ]] && rm -rf $1
}

tmpFile="insert.tmp"
deleteTmpFile $tmpFile

find ... | while read f
do
    #...
    echo "db.test.insert({'val': BinData(0, '$data')});" >> $tmpFile
done

cat insert.tmp|mongo -quiet --host 127.0.0.1:36427 collectionName
deleteTmpFile $tmpFile

Upvotes: 0

Related Questions