Can I create a new database simply by connecting to the MongoDB server, or is there another way to create it using Python? If so, how is this done?
4 Answers
MongoDB creates databases and collections automatically for you if they don't exist already.
For using python library with MongoDB, check out this documentation.
Warning: the example is based on Pymongo 2.1. If you're using Pymongo 3.4, check this doc.
from pymongo import Connection
connection = Connection()
db = connection['test-database']
collection = db['test-collection']
So here you can use any name for database and collection.
-
45An important note: neither the database nor the collection are created until you attempt to write a document. That's adequate for most applications, but it's good to know. Commented May 16, 2014 at 15:04
-
3I just read the Connection class is deprecated and you should use MongoClient instead: stackoverflow.com/questions/13959790/… Commented Jul 10, 2018 at 21:26
-
1This answer is not up to date. See the one below: stackoverflow.com/a/42039275/6655465 Commented Nov 8, 2021 at 11:18
This is Mongodb 3.4 Version:
from pymongo import MongoClient
client = MongoClient()
db = client.primer
coll = db.dataset
neither the database nor the collection are created until you attempt to write a document.
Document: Python Driver (PyMongo)
-
1Thumbs up for specifying the Pymongo version :)! The way that Pymongo works from version 2.1 (example above) to 3.4, are totally different. Commented Apr 28, 2017 at 22:57
For pymongo==4.3.3:
from pymongo import MongoClient
client = MongoClient()
print(client.list_database_names())
db = client["new_db"]
collection = db["new_collection"]
print( db.list_collection_names())
the database and collection are only going to be created when attempt to write a document.
Needed to create a collection without inserting the documents in order to be able to set validators first.
Luckily the pymongo Database object defines create_collection method:
create_collection(name, codec_options=None, read_preference=None, write_concern=None, read_concern=None, session=None, **kwargs)
Create a new Collection in this database.
Normally collection creation is automatic. This method should only be used to specify options on creation. CollectionInvalid will be raised if the collection already exists.
...
Changed in version 3.11: This method is now supported inside multi-document transactions with MongoDB 4.4+.
Changed in version 3.6: Added session parameter.
Changed in version 3.4: Added the collation option.
Changed in version 3.0: Added the codec_options, read_preference, and write_concern options.
Changed in version 2.2: Removed deprecated argument: options
-
Could you please add an example on collection creation with validator options ? ( I donc see "validator" or "timeseries" options in this doc :/ )– GuianCommented Aug 16 at 10:17
-
that's a different topic, but you need to use something like this:
database.command({'collMod': collection.name, 'validator': {'$jsonSchema': new_validator}})
Commented Aug 19 at 9:22