
- 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
Find a strict document that contains only a specific field with a fixed length?
You can use $where operator for this. Let us first create a collection with documents −
>db.veryStrictDocumentDemo.insertOne({"StudentFirstName":"John","StudentLastName":"Doe","StudentAge":23}); { "acknowledged" : true, "insertedId" : ObjectId("5cda4bcdb50a6c6dd317adb8") } > db.veryStrictDocumentDemo.insertOne({"StudentFirstName":"Larry"}); { "acknowledged" : true, "insertedId" : ObjectId("5cda4bdbb50a6c6dd317adb9") } >db.veryStrictDocumentDemo.insertOne({"StudentFirstName":"David","StudentLastName":"Miller"}); { "acknowledged" : true, "insertedId" : ObjectId("5cda4becb50a6c6dd317adba") } > db.veryStrictDocumentDemo.insertOne({"StudentFirstName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5cda4bfbb50a6c6dd317adbb") } > db.veryStrictDocumentDemo.insertOne({"StudentFirstName":"Bob","StudentLastName":"Brown"}); { "acknowledged" : true, "insertedId" : ObjectId("5cda4c6db50a6c6dd317adbc") }
Following is the query to display all documents from a collection with the help of find() method −
> db.veryStrictDocumentDemo.find();
This will produce the following output −
{ "_id" : ObjectId("5cda4bcdb50a6c6dd317adb8"), "StudentFirstName" : "John", "StudentLastName" : "Doe", "StudentAge" : 23 } { "_id" : ObjectId("5cda4bdbb50a6c6dd317adb9"), "StudentFirstName" : "Larry" } { "_id" : ObjectId("5cda4becb50a6c6dd317adba"), "StudentFirstName" : "David", "StudentLastName" : "Miller" } { "_id" : ObjectId("5cda4bfbb50a6c6dd317adbb"), "StudentFirstName" : "Chris" } { "_id" : ObjectId("5cda4c6db50a6c6dd317adbc"), "StudentFirstName" : "Bob", "StudentLastName" : "Brown" }
Following is the query to get a specific field with fixed length −
> db.veryStrictDocumentDemo.find({ "StudentFirstName": { $exists: true }, "StudentLastName": { $exists: true }, $where: function() { return Object.keys(this).length === 4 } });
This will produce the following output −
{ "_id" : ObjectId("5cda4bcdb50a6c6dd317adb8"), "StudentFirstName" : "John", "StudentLastName" : "Doe", "StudentAge" : 23 }
- Related Articles
- Find document with array that contains a specific value in MongoDB
- Find which MongoDB document contains a specific string?
- Find MongoDB documents that contains specific field?
- Find the document by field name with a specific value in MongoDB?
- MongoDB query select and display only a specific field from the document?
- Find documents that contains specific field in MongoDB?
- How to select MongoDB document that does not consist a specific field?
- Update only a specific value in a MongoDB document
- How to select objects where an array contains only a specific field in MongoDB?
- Extract a MongoDB document with a specific string
- MongoDB query to fetch a specific document rom documents with field value set using NumberInt()?
- How to add a field with specific datatype (list, object) in an existing MongoDB document?
- Find documents with arrays not containing a document with a particular field value in MongoDB?
- Set MongoDB compound index with a fixed value field
- Fetch a specific document in MongoDB with array elements

Advertisements