MongoDB stores BSON files as data records into the collection; The following is the collection in the database.
First, Databases
In MongoDB, the database saves a document-style collection.
In the MONGO shell, if you want to select a certain database to use, you can use the using command, as in the following example:
Use MyDB
1.1 Creating a database Create a databases
If the database does not exist, MongoDB will create this database the first time you store data for this database. Therefore, you can switch to a nonexistent database and execute the following command in the MONGO Shell:
Use mynewdb
Db.myNewCollection1.insertOne ({x:1})
where the Insertone () operation creates both the mynewdb database and the MyNewCollection1 collection Even if they do not exist at first.
For a list of database name restrictions, name restriction.
Second, set collections
MongoDB stores the document into the collection. Collections are similar to tables in relational databases.
2.1 Creating a collection Create a Collection
If the collection does not exist at the beginning, MongoDB will create this collection when you first store the data to this collection.
Db.myNewCollection2.insertOne ({x:1})
Db.myNewCollection3.createIndex ({y:1})
For a list of restricted names for the collection, click View [Name Restrictions] (
https://docs.mongodb.com/manual/reference/limits/#restrictions-on-collection-names).
2.2 Creating an explicit
MongoDB provides db. The CreateCollection () method explicitly creates a collection with various option settings, such as setting a maximum size or a document validation rule. If you do not specify these options, you do not need to explicitly create a collection because MongoDB creates a new collection when you first store the data for collecitons.
If you want to modify the option settings for these collections, view the Collmod collection template.
2.3 Document Validation
New in version 3.2.
By default, a collection does not require a document to have the same schema, that is, a document in a collection does not need to have the same set of fields, and the data type of each field can be different in one collection.
Starting with MongoDB 3.2, you can apply document validation rules when you make updates or insert data into a collection. See document validation for more details.
2.4 Modifying the document schema
You can modify the schema of a document in a collection, such as adding new fields, removing old fields, modifying the data type of a field, and eventually updating the document into a new schema.
Null
Databases and collections (MongoDB document translation and interpretation)