2

I have a MongoDB collection filled with dummy data for a demo. I have an image file in a folder called example.jpg For each ID in the collection I want to create a symlink from ID.jpg to example.jpg

For example if the collection holds 3 documents with ids of 345kjh34k5, 8945ng49, and 3459t8u34 I want to create 3 symbolic links of 345kjh34k5.jpg, 8945ng49.jpg and 3459t8u34.jpg that all point to example.jpg

Thanks!

1 Answer 1

2

You would want to create a mongo script prints the ids, then have a bash script that loops through it and creates a link for each id.

Here's some code for it.

Setup - create Mongodb Dummy collection

db = connect("test");
db.dummy.insert({ _id : "345kjh34k5" });
db.dummy.insert({ _id : "8945ng49" });
db.dummy.insert({ _id : "3459t8u34" });

Setup - Create sample file

Filename: file1.txt

File1 content. Blah blah blah.

MongoDB script print dummy ids

Filename: printDummyIds.js

db = connect("test");
var linkNames = db.dummy.find().map(function(doc){
        print( doc._id );
});

Output:

mongo --quiet printDummyIds.js 
345kjh34k5
8945ng49
3459t8u34

Bash script that calls mongo then creates soft links

Filename: createLinksFromIds.sh

echo Linking files to source.
echo Source: $1
ext="${1#*.}"
out="./out/"
mkdir $out
for id in $(mongo --quiet printDummyIds.js) 
do 
    lName=$out$id"."$ext ;
    echo Creating $lName ;
    ln -s $1 $lName ;
done

Usage

createLinksFromIds.sh FILE_PATH
FILE_PATH is the file that you want to link.

Example:

bash createLinksFromIds.sh file1.txt

Output:

Linking files to source.
Source: file1.txt
Creating ./out/345kjh34k5.txt
Creating ./out/8945ng49.txt
Creating ./out/3459t8u34.txt
Sign up to request clarification or add additional context in comments.

Comments

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.