EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home Data Science Data Science Tutorials MongoDB Tutorial MongoDB schema

MongoDB schema

Updated March 8, 2023

MongoDB schema

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.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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 ()

mongodb schema 1

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.

mongodb schema 2

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.

mongodb schema 3

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.

mongodb schema 4

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.

image 1

image 2

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.

example 4

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 ()

12

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) ; }

image 3

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 –

  1. MongoDB Join
  2. MongoDB findOne()
  3. MongoDB group by
  4. MongoDB Database
C++ PROGRAMMING Course Bundle - 9 Courses in 1 | 5 Mock Tests
37+ Hour of HD Videos
9 Courses
5 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
ASP.NET Course Bundle - 28 Courses in 1 | 5 Mock Tests
123+ Hours of HD Videos
28 Courses
5 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
SQL Course Bundle - 51 Courses in 1 | 6 Mock Tests
204+ Hours of HD Videos
51 Courses
6 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
SOFTWARE TESTING Course Bundle - 13 Courses in 1
53+ Hour of HD Videos
13 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Popular Course in this category
MONGODB Course Bundle - 6 Courses in 1
 20+ Hours of HD Videos
6 Courses
Verifiable Certificate of Completion
  Lifetime Access
4.5
Price

View Course
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more