Windows

如何使用 pymongo 創建新的數據庫和集合?

  • February 21, 2019

我想使用 python 腳本創建一個數據庫和集合,以便我使用 pymongo 驅動程序。但我無法創建數據庫。

我遵循的步驟:

from pymongo import MongoClient
con = MongoClient('localhost', 27017)
db = con.testdb

在我簽入 mongoshell 時執行此命令後,未創建數據庫“testdb”。

我可以使用 paramiko 與 mongo shell 互動嗎?

蟒蛇版本:2.7

來自http://api.mongodb.com/python/current/tutorial.html

它指出

An important note about collections (and databases) in MongoDB is that 
they are created lazily - none of the above commands have actually 
performed any operations on the MongoDB server. **Collections and 
databases are created when the first document is inserted into them.**

引用自:https://unix.stackexchange.com/questions/434791