Is there any way to use my local mongodb server on google colab I can't access it with the local host link it is creating a new database as It has different IP address maybe
import datetime
import random
import string
import uuid
import time
from pymongo import MongoClient
class DataObj:
def __init__(self, db, collection):
self.db = db[collection]
self.collection = collection
self.id = str(uuid.uuid4())[:6]
def insert(self, n_sensors):
keys_data = [self.collection + '_id','room_name','temperature', 'date']
list_data = []
list_room_name = ['bedroom','livingroom','guestroom','kitchen']
instrument = ['AirCondition','fan','refrigerator']
for i in range(n_sensors):
room_name= random.choice(list_room_name)
instrument_name = random.choice(instrument)
value1 = self.id + room_name+ 'sns' + str(i)
value2 = random.uniform(85.5,90)
date = datetime.datetime.now()
temperature = {'sensor_id': value1,'instruments':instrument_name,'temperature':value2 }
json = dict(zip(keys_data, [self.id,room_name ,temperature, date]))
# print(json)
list_data.append(json)
self.db.insert_many(list_data)
client = MongoClient('mongodb://localhost:27017/')
db = client['smart-housedb']
house_1 = DataObj(db, 'house_1')
house_2 = DataObj(db, 'house_2')
s = time.time()
# for j in range(10):
# house_1.insert(3)
# house_2.insert(5)
e = time.time()
# print(e-s)
def fiveHundredMb():
house_1.insert(3)
house_2.insert(5)
dicti = db.command("dbstats")
t1 = time.time()
while int(dicti["totalSize"]) <= 500000000:
fiveHundredMb()
dicti = db.command("dbstats")
# print(dicti["totalSize"])
t2 = time.time()
print(t2-t1)
This is my local code which I ran on m y visual studio code it is running and I can aches it through my terminal with mongodb queries but when I run the same code in google colab it asked me to start mongo server services and it is creating new database, is their any chance to run same database on my google colab?
Localhost means your DB is not accessible online. Google collab is a cloud programming service, this means you run code on a server. Your code on Google collab will not be able to have access to your local DB. you should create an online DB, and then Google collab will have access to it. Here you can create an online MongoDB for free.