Create indexes on initdb script - mongodb

I have a custom docker image from mongo:4.2-6-bionic with two initdb scripts:
/docker-entrypoint-initdb.d/1-database-seed.sh
/docker-entrypoint-initdb.d/2-create-indexes.sh
When the image is created, the 1-database-seed.sh file is executed correctly. But, 2-create-indexes.sh is not executed.
If i execute manually 2-create-indexes.sh, inside the container, the script runs correctly. The script content is:
#!/bin/bash
mongo renaper --eval "db.names_indexed.createIndex({'fullname': 1})";
mongo renaper --eval "db.names_indexed.createIndex({'year': 1})";
mongo renaper --eval "db.names_indexed.createIndex({'fullname': 1,'year': 1})";
mongo renaper --eval "db.names_indexed.createIndex({'fullname': 'text'})";
Any ideas ?

Finally, I put all the tasks in a single file and it worked correctly.

Related

Use [DB] for Mongodb with shell

I am researching now the simple "use" command for the mongo command. Please help me.
I just want save the my query in a file, but before that i need to connect to a certain database. For that i tried to find a "use" command like in sql, but could not find anything.
I just want to execute something like
mongo ....--use [db] --eval 'db.find' > save.query
In your question, you didn't specify what platform you're using, are you using Linux? Windows? Anyway, if you want to use the command line for mongo db then I would recommend to use the mongodb shell. Download the mongodb shell https://www.mongodb.com/try/download/shell and select what platform you're using.
https://www.mongodb.com/docs/mongodb-shell/run-commands/#mongosh-usage
That sais you have use <database> command
I just got it. You can just add the database name:
mongo [dbname] --host etc.
and it worked.
This is the easiest way in linux:
Option 1 ( command line params )
echo "db.exampleollection.find({}).forEach(function(d){printjson(d)})" | mongo --quiet exampledatabase --host "examplehost" --port "examplePort" --authenticationDatabase=admin -u "exampleuser" -p "examplepassword" > output.json
Explained:
Send the command you need to execute via echo to the mongo shell and redirect the output to the result file.
Add the option --quiet to suppress the shell printed info
You can provide the database directly in as comman line argument.
Option 2 : same way but URI format:
echo "show collections" | mongo "mongodb://user:pass#host:port/database?authSource=admin" --quiet

Mongo --ssl on bash script

I'm writing a bash script where it connects to the mongodb in different ways and I'll run this script on various projects - some of them require --ssl connection and some of them don't. So, I wanted to know a way for me to maybe declare a variable on top which will turn on or off depending on whether the project needs --ssl connection.
ssl="--ssl" #how do I determine whether to turn this variable on or off depending on whether the project needs --ssl?
Example of where its used in bash script
`master_var=`mongo ${ssl_mode} --eval "db.isMaster.ismaster"`
Another example in the bash script where I connect to mongo:
mongo --quiet ${ssl_mode} ${name_db} <<EOF
#some commands
EOF
Edit: I want all of this to be done on the bash script itself.
You can use environment variables:
if [ -n "$MYSCRIPT_ENABLE_SSL" ]; then
ssl_mode="--ssl"
fi
And from where you call the script:
MYSCRIPT_ENABLE_SSL=1 ./myscript.sh
or
export MYSCRIPT_ENABLE_SSL=1
./myscript.sh

mongodb dropped collection keeps coming back after mongodump and mongorestore

I have just created a data set, but a collection, 'items', is still lurking around and I am getting rid of it with the command db.items.drop() (after exec'ing into my docker container), which seems to successfully remove it. But when I run the 2 following scripts (after removing the collection):
save script:
#!/usr/bin/env bash
MONGO_DB_NAME="test-1"
DATA_NAME="test-data"
docker exec ${MONGO_DB_NAME} mongodump --quiet --out /tmp/${DATA_NAME}
mkdir -p ./backup
docker cp ${MONGO_DB_NAME}:/tmp/${DATA_NAME} ./backup/${DATA_NAME}
docker exec ${MONGO_DB_NAME} rm -rf /tmp/${DATA_NAME}
restore script:
#!/usr/bin/env bash
MONGO_DB_NAME="test-1"
DB_NAME="docker-node-mongo"
DATA_NAME="test-data"
docker exec -it $(docker ps -aqf "name=${MONGO_DB_NAME}") mongo ${DB_NAME} --eval "db.dropDatabase();"
docker cp ./backup/$DATA_NAME $(docker ps -aqf "name=${MONGO_DB_NAME}"):/tmp
docker exec -it $(docker ps -aqf "name=${MONGO_DB_NAME}") mongorestore --drop /tmp/${DATA_NAME}
the items collection comes right back. Does anyone know why this is? I'd like it gone for good.
Edit: When I delete individual items from the collection, and run the above scripts, they do not come back. But when I drop the collection as a whole, the collection keeps coming back (with nothing in it).
The mongodump command does not delete files that already exist in the --out path. Files for collections that are being backed up will be overwritten, but files for collection that no longer exist will not be touched, leaving the 'items' file(s) there to be restore the next time you run mongorestore.
To make sure this doesn't happen, move aside or remove the existing backup directory and create a new, empty one before running mongodump.
I had exactly similar "haunting" collection, which kept coming back again and again. The haunting collection also didn't work ok (e.g. cloning within it caused always an error). I don't know what caused it, but from some console logs I finally noticed that somehow another collection was using it's Name. I was then able to fix it by renaming the collection via mongosh and deleting then:
db.rrecord.renameCollection("record")
"This operation will rename the rrecord collection to record. If the target name (i.e. record) is the name of an existing collection, then the operation will fail."
ref. https://docs.mongodb.com/manual/reference/method/db.collection.renameCollection/

COPY command not found in postgres dockerize

I am trying to COPY data from CSV to container
From postgres container shell
$COPY
copy command not found
also tried \copy
but createdb and dropdb works for me.
COPY is not a bash shell command, it's a command run within the psql database query shell.

How to fill in a Docker mongodb image

I don't really understand how I can fill in my mongodb image with a simple script like demoInstall.js.
I have my image and I can launch a mongodb instance. But I can not access to the docker image "mongo shell" to fill this one with the script.
I tried this command :
sudo docker run --entrypoint=/bin/cat mongo/ubuntu /tmp/devInstall.js | mongo --host IPAdress
But it's using the local mongo and not the image :/
Finally my aim is simple, I need to pull my image on a virgin server and launch a basic bash script who fill some informations in the Docker db.
The command you use does pipe on the output of the docker locally. You might call with explicit bash -c instead:
sudo docker run -it --rm mongo/ubuntu /bin/bash -c '/bin/cat /tmp/devInstall.js | mongo --host IPAdress'
I am not sure the IPAdress will be available though. You might want to define it via environmental parameter or container linking.
I would mount a volume with this argument:
-v /local_init_script_folder:/bootstrap
And then with a similar commandline like you proposed, you cann access the contents of the folder as /bootstrap from within the container.