import pymongo
import uuid
import datetime
class Database:
def __init__(self,myclient,mydb,mycol):
self.blog_id = input('Enter Blog ID : ')
self.title = input('Enter Title : ')
self.content = input('Enter Content : ')
self.author = input('Enter Your Name : ')
self.created_date = datetime.datetime.utcnow()
self.ids=uuid.uuid4().hex
def json(self):
return {
'id':self.ids,
'blog_id':self.blog_id,
'author':self.author,
'content':self.content,
'title':self.title,
'created_date':self.created_date
}
def insert(self,data):
mydb.mycol.insert_one(data)
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["posts"]
dataBase = Database(myclient,mydb,mycol)
data = dataBase.json()
dataBase.insert(data)
当我进入MongoDB并尝试使用
databaseName.collectionName.find({})
命令,它说
E QUERY [js] ReferenceError: mydatabase is not defined : @(shell):1:1