
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
Perform simple validation in MongoDB?
For validation in MongoDB, use validator. Following is the query to create validation on collection in MongoDB −
> db.createCollection( "demo437" , { ... validator: { $jsonSchema: { ... bsonType: "object", ... required: [ "FirstName", "LastName"], ... properties: { ... FirstName: { ... bsonType: "string", ... description: "This is required" }, ... LastName: { ... bsonType: "string", ... description: "This is required" } ... ... } ... }}}) { "ok" : 1 }
Let us create a collection with documents −
> db.demo437.insertOne({"FirstName":"John","LastName":1234}); 2020-03-22T17:49:55.590+0530 E QUERY [js] WriteError: Document failed validation : WriteError({ "index" : 0, "code" : 121, "errmsg" : "Document failed validation", "op" : { "_id" : ObjectId("5e7757ebbbc41e36cc3cae9a"), "FirstName" : "John", "LastName" : 1234 } }) WriteError@src/mongo/shell/bulk_api.js:461:48 Bulk/mergeBatchResults@src/mongo/shell/bulk_api.js:841:49 Bulk/executeBatch@src/mongo/shell/bulk_api.js:906:13 Bulk/this.execute@src/mongo/shell/bulk_api.js:1150:21 DBCollection.prototype.insertOne@src/mongo/shell/crud_api.js:252:9 @(shell):1:1 > > db.demo437.insertOne({"FirstName":"John","LastName":"Smith"}); { "acknowledged" : true, "insertedId" : ObjectId("5e7757f7bbc41e36cc3cae9b") } > db.demo437.insertOne({"FirstName":9887,"LastName":"Miller"}); 2020-03-22T17:50:29.631+0530 E QUERY [js] WriteError: Document failed validation : WriteError({ "index" : 0, "code" : 121, "errmsg" : "Document failed validation", "op" : { "_id" : ObjectId("5e77580dbbc41e36cc3cae9c"), "FirstName" : 9887, "LastName" : "Miller" } }) WriteError@src/mongo/shell/bulk_api.js:461:48 Bulk/mergeBatchResults@src/mongo/shell/bulk_api.js:841:49 Bulk/executeBatch@src/mongo/shell/bulk_api.js:906:13 Bulk/this.execute@src/mongo/shell/bulk_api.js:1150:21 DBCollection.prototype.insertOne@src/mongo/shell/crud_api.js:252:9 @(shell):1:1 > > > db.demo437.insertOne({"FirstName":"David","LastName":"Miller"}); { "acknowledged" : true, "insertedId" : ObjectId("5e775816bbc41e36cc3cae9d") }
Display all documents from a collection with the help of find() method −
> db.demo437.find();
This will produce the following output −
{ "_id" : ObjectId("5e7757f7bbc41e36cc3cae9b"), "FirstName" : "John", "LastName" : "Smith" } { "_id" : ObjectId("5e775816bbc41e36cc3cae9d"), "FirstName" : "David", "LastName" : "Miller" }
- Related Articles
- How to perform Schema Registration and XML Validation in Oracle ?
- How to generate JSON data and perform Schema Validation in Oracle?
- Learn How to Perform Simple Magic Tricks
- Perform aggregation sort in MongoDB?
- Perform Bulk insert in MongoDB?
- Filter documents in MongoDB using simple query?
- Perform MongoDB full text search
- Perform conditional upserts or updates in MongoDB
- Perform nested document value search in MongoDB?
- Perform min/max with MongoDB aggregation
- How to perform ascending order sort in MongoDB?
- How to perform descending order sort in MongoDB?
- Perform MongoDB array concatenation to concatenate records
- How to perform different simple thresholding of an image using Python OpenCV?
- MongoDB query to change simple field into an object?

Advertisements