'Trying to use MongoDB with Google Colab by placing the data directory on my Google Drive

I want to use MongoDB from Google Colab. So I need to be able to put my MongoDB data directory on my Google Drive so I don't lose my data from one session to another, but I cannot change the data folder.

  1. Mount Google Drive
# Mount Google Drive
from google.colab import drive drive.mount('/content/drive')
  1. Install MongoDB on Colab environment
!apt-get install mongodb
  1. Stopping the MongoDB service
!service mongodb stop
  1. Relaunching the MongoDB service with parameter dbpath to specify the new path
!service mongodb start --dbpath /content/drive/MyDrive/mongodb
  1. Using pumongo lib to access database after
import pymongo from pymongo
import MongoClient client = MongoClient()
client.list_database_names() # which return ['admin', 'local']

The problem with my code above is that I can query my Mongo database, but mongodb will still use the path specified in the mongodb.conf file.

# Where to store the data.
dbpath=/var/lib/mongodb

Then I tried to change the path in the mongodb.conf file to

# Where to store the data.
dbpath=content/drive/MyDrive/mongodb

But then I get an error when I restart the mongodb service

* Starting database mongodb
    ...fail!`

What am I forgetting or not doing right at all please?

Thanks for your help



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source