mongo - databases

Cheatsheets

# Mongoengine (python/flask)
https://cheatography.com/amicheletti/cheat-sheets/mongoengine/

# Mongo
https://cheatography.com/ovi-mihai/cheat-sheets/mongodb/
        

MongoEngine Querying

MyDocument.objects
MyDocument.objects(key=value)
MyDocument.objects.count()
MyDocument.objects(key__op=value)
        

Use Docker To Create Local MongoDB

# Fresh instance
docker run -p 27017:27017 mongo

# Attached to persistent storage
docker run -p 27017:27017 -v /my/own/datadir:/data/db mongo
        

Upload Existing Database To Server

# This requires a local mongod instance to be running

# Dump the database
mongodump -d db_name -o ~/example_directory/

# Restore the database on the server
mongorestore --uri '<connection_string>' -d <db_name_to_upload_to> ~/example_directory/
NOTE: If you specified db in the connection string then simply delete that word; should be admin or something generic
NOTE: If you get a DNS error, change your /etc/resolv.conf nameserver variable to 8.8.8.8 or 1.1.1.1 
        

Use libcurl3 while having libcurl4 installed

Ever find yourself with this error? (or something similar)
    mongo: /usr/lib/x86_64-linux-gnu/libcurl.so.4: version `CURL_OPENSSL_3' not found (required by mongo)

1. Run these commands to download the libcurl.so.3 binary:

# Create a temporary libcurl3 directory in your home path and go there
mkdir ~/libcurl3 && cd ~/libcurl3
# Download the libcurl3 package to the newly created folder
apt-get download -o=dir::cache=~/libcurl3 libcurl3
# Extract a file named data.tar.xz from that package archive
ar x libcurl3* data.tar.xz
# Extract the data.tar.xz file to the current folder
tar xf data.tar.xz
# Copy the referenced file of the libcurl.so.4 symlink to /usr/lib/libcurl.so.3 (don't mind the .4 suffix, as noted it is actually version 3)
cp -L ~/libcurl3/usr/lib/x86_64-linux-gnu/libcurl.so.4 /usr/lib/libcurl.so.3
# Go back home and remove the temporary directory from your home path
cd && rm -rf ~/libcurl3


2. Then setting the env var LD_PRELOAD we can now load mongo
env LD_PRELOAD=/usr/lib/libcurl.so.3 mongo

With that being said, you can create a mongo alias in your .bashrc file
and now use mongo as a standalone command:
alias mongo="env LD_PRELOAD=/usr/lib/libcurl.so.3 mongo"
        

Export Database From Server To Local Machine

# Dump remote database to a local folder
mongodump --uri "mongodb+srv://username:password@host/mydb" --out ./myfolder

# Restore the database from the folder to local mongo server
mongorestore --port 27017 ./myfolder -d output-db