Skip to content
Home » mongodb

mongodb

FIX MONGODB GPG ERROR ON UBUNTU

From time to time you will need to update your Mongo pgp key so you can run normal updates. Below is the procedure

When running
sudo apt-get update

You will receive an error such as this when the pgp keys for Mongo are out of date
W: GPG error: . and so on
E: The repository ‘https://repo.mongodb.org/apt/……..’ is not signed

To fix this run the following:
wget -qO – https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add –

You will want to change the 5.0 above to the version listed in the error like this:
wget -qO – https://www.mongodb.org/static/pgp/server-4.2.asc | sudo apt-key add –

Press Enter
Mongo will get the new keys

Now you can run
sudo apt-get update
and you will not receive the errors

INSTALL MONGODB ON RASPBERRY PI 4

By far the best guide I have found, in starting this journey, was written by Mark Smith on https://developer.mongodb.com/how-to/mongodb-on-raspberry-pi/

If you want to see why things work the way they do, please visit his guide. My guide below is just the commands, and how I am connecting into the database once it is setup.

To begin, first you will want to install Ubuntu Server 20.10 as described here:
https://tacticalware.com/install-ubuntu-server-20-10-64-bit-on-raspberry-pi-4-using-a-m-2-drive/

Once you are up and running, ssh into the Raspberry Pi
Login

From the terminal run the following:
sudo bash

wget -qO – https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add –

echo “deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse” | tee /etc/apt/sources.list.d/mongodb-org-4.4.list

apt-get update

apt-get install -y mongodb-org

systemctl daemon-reload

systemctl enable mongod

systemctl start mongod

systemctl status mongod

CTRL button +c to exit

mongo
use admin

db.createUser( { user: "admin",
 
        pwd: "SuperSecretPassword",

        roles: [ "userAdminAnyDatabase",

                 "dbAdminAnyDatabase",

                 "readWriteAnyDatabase"] } )

exit

nano /etc/mongod.conf

Add these lines to the end of the file

security:

   authorization: enabled

CTRL +x to Exit
Y to Save

systemctl restart mongod

mongo

db.adminCommand({listDatabases: 1})

Make sure you had no errors

exit

mongo -u “admin” -p

Enter your admin password for mongo, aks your SuperSecretPassword

Type
db.adminCommand({listDatabases: 1})
This will list out your databases and ensure you created it properly

exit

nano /etc/mongod.conf
and Change the bind ip from 127.0.0.1 to 0.0.0.0

net:
   port: 27017
   bindIp: 0.0.0.0

CTRL +x to Exit
Y to Save

systemctl restart mongod

ufw allow 27017/tcp

MongoDB is now successfully setup and running. On to configuring the Windows 10 system

To connect to the MongoDB from a Windows 10 computer

Open an Internet Browser on your Windows 10 computer
Navigate to https://robomongo.org/
Download the Robomongo Robo 3T Client
Once you install it, add these settings to connect to your system
Click Create
Connection Tab
Type – Direct Connection
Address – The IP of your Raspberry Pi
Port 27017
Authentication Tab
Check mark Perform Authentication
Database is admin
User Name is admin
Password is your SuperSecretPassword
Auth is SCRAM
Click Save
Click Connect

You are now connected and ready to run with your MongoDB environment

Hardware that I used:
Raspberry Pi 4 (4gb)
https://amzn.to/3q551IO

CanaKit 3.5A Raspberry Pi 4 Power Supply (USB-C)
https://amzn.to/3fNTYPu

CanaKit Raspberry Pi 4 Micro HDMI Cable – 6 Feet
https://amzn.to/33u5hr9

Western Digital 500GB WD_Black SN750 NVMe
https://amzn.to/3nZ5pH4

Plugable USB C to M.2 NVMe Tool-free Enclosure
https://amzn.to/3lflV3L

COMBINE MULTIPLE CSV FILES FROM THE COMMAND LINE AND RETAIN FILENAMES

For this guide we will take thousands of csv files and combine them into One single file, which will be uploaded to a database. This file will have a new column inserted, where the originating filename will be added next to the row. In scraping sites, often my output files are separated and after I clean them up I need to combine them. This is how I do it.

On my Windows 10 computer
I will copy my CSV files over to a secondary drive, leaving the originals intact.
Then I will open a command prompt as an administrator and navigate to that drive. If the files are on my H: drive will navigate by typing
H:
Then press Enter

Now I will navigate to the “dataset” folder
cd dataset

I will then list the directory contents to ensure this holds my csv files
dir

Once I see all of my csv files I will enter the following:

for /f %a in ('dir /b *.csv') do for /f "tokens=*" %b in (%a) do echo %b,%a >> combined-date.csv

Then press Enter

This will create a file called combined-04132021.csv (which is today’s date)

And I can then upload that file to my database easily

COMBINE MULTIPLE CSV FILES FROM THE COMMAND LINE

For this guide we will take thousands of csv files and combine them into One single file, which will be uploaded to a database. In scraping sites, often my output files are separated and after i clean them up I need to combine them. This is how I do it.

On my Windows 10 computer
I will copy my CSV files over to a secondary drive, leaving the originals intact.
Then I will open a command prompt as an administrator and navigate to that drive. If the files are on my H: drive will navigate by typing
H:
Then press Enter

Now I will navigate to the “dataset” folder
cd dataset

I will then list the directory contents to ensure this holds my csv files
dir

Once I see all of my csv files I will enter the following
copy *.csv combined-date.csv
Then press Enter

This will create a file called combined-04132021.csv (which is today’s date)

And I can then upload that file to my database easily