简体   繁体   English

如何使用 Python 在 Mongo 中创建数据库?

[英]How to create a database in Mongo using Python?

I have written the below Python program to create a MongoDB.我编写了下面的 Python 程序来创建 MongoDB。

import pymongo

myclient = pymongo.MongoClient('mongodb://localhost:27017/')

mydb = myclient['mydatabase']

dblist = myclient.list_database_names()

if "mydatabase" in dblist:
  print("The database exists.")
else :
  print("The database does not exists.")

But while I execute the program, I get the result as但是当我执行程序时,我得到的结果是

The database does not exists.数据库不存在。

Why so?为什么这样? Is there something that I am missing?有什么我想念的吗?

I am following the code mentioned in W3Schools Tutorial我正在遵循W3Schools 教程中提到的代码

In MongoDB the database is not actually created until you put some data into it, try put a document inside and check again.在 MongoDB 中,在您将一些数据放入其中之前,该数据库并未真正创建,请尝试将文档放入其中并再次检查。

Important: In MongoDB, a database is not created until it gets content!重要提示:在 MongoDB 中,在获取内容之前不会创建数据库!

Databases are lazily created.数据库是延迟创建的。 Insert something into a collection in that database and they (database/collection) will spring into existence.将某些内容插入该数据库的集合中,它们(数据库/集合)将 spring 存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM