
- 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
How to validate documents before insert or update in MongoDB?
To validate documents, use the concept of validation. Following is the query −
> db.createCollection("demo356", {validator: { ... $and: [ {"FirstName": {$type: "string", $exists: true}} ] ... }}) { "ok" : 1 }
Let us create a collection with documents −
> db.demo356.insertOne({"FirstName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e568d49f8647eb59e5620c7") } > db.demo356.insertOne({"FirstName":909}); 2020-02-26T20:52:58.497+0530 E QUERY [js] WriteError: Document failed validation : WriteError({ "index" : 0, "code" : 121, "errmsg" : "Document failed validation", "op" : { "_id" : ObjectId("5e568d52f8647eb59e5620c8"), "FirstName" : 909 } }) 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
Display all documents from a collection with the help of find() method −
> db.demo356.find();
This will produce the following output −
{ "_id" : ObjectId("5e568d49f8647eb59e5620c7"), "FirstName" : "Chris" }
- Related Articles
- How to update all documents in MongoDB?
- How to update multiple documents in MongoDB?
- How to update or modify the existing documents of a collection in MongoDB?
- How to update many documents with one query in MongoDB?
- MongoDB bulk insert for documents
- Get documents expired before today in MongoDB?
- How to Update multiple documents in a MongoDB collection using Java?
- MongoDB - update partial number of documents?
- How to insert new documents into a MongoDB collection in your database?
- MongoDB query to update all documents matching specific IDs
- Update objects in a MongoDB documents array (nested updating)?
- Update values in multiple documents with multi parameter in MongoDB?
- MongoDB to fetch documents with $or Operator
- MongoDB query to update each field of documents in collection with a formula?
- How to ceate MySQL Trigger before Insert?

Advertisements