Updated March 8, 2023
Definition of MongoDB schema
MongoDB schema basically used in command-line tool or we can use it programmatically in our application at a module level. As we already know MongoDB is schema-less, at the time of creating any objects we cannot create any schema in MongoDB. We can enforce the schema for collection in MongoDB by using the MongoDB atlas cluster, for enforcing the document schema we need first to connect the database and collection. We can also create schema structure of collection by using create collection command, also we can check the index and collection field in MongoDB.
Syntax:
Below is the syntax of the MongoDB schema.
1) To show the schema of index collection –
db.name_of_collection.getIndices ()
2) To show the schema of collections –
var schematodo = db.name_of_collection.findOne()
for (var key (Used to show key fields) in schematodo (Used to display schema field of collections)) { print (key, typeof key) ; }
3) Schema structure of document –
{
Properties: {
Field_name: {bsonType (type of document) : string}
Field_name: {bsonType (type of document) : string}
}
}
Parameter Description:
1) Name of collection – This is defined as the name of collection from which we have checked the schema structure of collection and indexes. We can check the schema structure of any collection in MongoDB.
2) getIndices – This is the method in MongoDB used to display schema structure of all indexes from specified schema which was we have used in our command.
3) findOne – This method is used to find single documents from collections. Using this method we also find all collection fields in MongoDB.
4) schematodo – This is used to display the schema structure of database collection in MongoDB. Using schematodo we can display all fields from collections.
5) Key – This parameter is defined as print the field from the specified collection which was we have used in our query.
6) Type of key – This parameter is defined as a type of key which was we have used in the query to display the schema structure.
7) Properties – This parameter is defined as the property of the document field which was we have used in our query.
8) Field name – This is defined as the name of the field which was we have used in our query. Using field name we can retrieve the document structure.
9) BSON type – This is defined as the document type which was we have used in the collection.
How schema works in MongoDB?
- MongoDB is schema-less structure but we can enforce the collection by defining the document schema.
- Schema is nothing but regular documents which was adhered to like the same specification of JSON schema.
- We can also validate the schema in the MongoDB server. We can also use the type key to control the collection field value.
- In MongoDB, document schema will represent any of the BSON type operators. We can define the schema structure of the following types.
1) Booleans
2) Numbers
3) Strings
4) Arrays
5) Objects
6) Dictionary’s
7) Sets
8) Mixed
9) ObjectId
10) UUID
To display the schema of indexes in MongoDB we need to first connect to the specific database. The below example shows that we need to connect the database to display the structure of indexes.
Code:
db.MongoDB_Update.getIndices ()
use test
db.MongoDB_Update.getIndices ()
Figure – We need to connect the database to display the structure of indexes
- In the above example when the first time execution of the query we have not connected to the database, so it will not show the result of the query. But after connecting to the specified database we can see the schema of indexes in MongoDB.
- Binary encoded superset will support the additional data types in MongoDB.
- We can enforce the document schema using MongoDB atlas. To create the enforcing schema first we need to connect the database and collections.
- We don’t create a collection with schema in MongoDB, we can create an empty collection in MongoDB.
- At the time of inserting documents, MongoDB automatically creates the schema for the collection.
- We can say that MongoDB is schema-less database but we can implement our own class in our program to restrict the collection before inserting any data into the collection.
Example
Below example shows that enforce collection document schema using MongoDB atlas. We have used the below steps to enforce document schema.
1) Create a new application or open the existing application
In the first step, we have created the application name as Application-3 and link the cluster database as MongoDBSchema. After defining the name of the application and linking to the database then click on create a new application.
Figure – Example to create new application to enforcing schema.
2) Add the collection and database
- After successfully creating the application add the database and collection to the specified application. First, click on add collection.
Figure – Example to add collection and database.
- After clicking on add collection add the database and collection to the application.
- We have added the sample_training database and grades table to the application.
Figure – Check collection and database added to the application.
3) Generate schema
After successfully adding the database and collection generate the schema. For generating schema click on the schema tab and select the collection name after selecting the collection name click on generate schema tab. After generating schema click on the save tab.
Figure – Generate schema.
4) Run the validation on generated schema
- In the below example we have to check our validation on grades collection is working or not.
- The first click on validate tab then select the sample documents which was greater than 1000.
Figure – Example to run validation on schema.
5) Display the index schema details
In the below example, we have displayed the schema structure of indexes. We have displayed all the indexes structures from MongoDB_Update collections.
Code:
db.MongoDB_Update.getIndices ()
Figure – Example to display index schema structure details.
6) Display the schema fields from collections
In the below example, we have a display the schema of the collection. We can see that all the fields from MongoDB_Update fields will be displayed.
Code:
var schematodo = db.MongoDB_Update.findOne()
for (var key in schematodo) { print (key, typeof key) ; }
Figure – Example to Display the schema fields from collections.
Conclusion
Basically, MongoDB is schema-less database, we cannot create schema in MongoDB, but we enforce the collection documents in application code or using MongoDB atlas GUI tool. For generating schema first we need to connect to the specified database and collections in MongoDB.
Recommended Articles
This is a guide to Mongodb schema. Here we discuss the definition, How schema works in Mongodb? along with examples respectively. You may also have a look at the following articles to learn more –