
- 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 documents with arrays not containing a document with a particular field value in MongoDB?
You can use $nin operator for this. To understand the concept, let us create a collection with the document. The query to create a collection with a document is as follows −
> db.documentWithAParticularFieldValueDemo.insertOne( ... { ... ... "StudentId" : 101, ... "StudentDetails" : ... [ ... { ... "TheoryMarks": 78, ... "PracticalMarks": 91 ... }, ... { ... "TheoryMarks": 75, ... "PracticalMarks": 75 ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5c8edab72f684a30fbdfd586") } > db.documentWithAParticularFieldValueDemo.insertOne( ... ... { "StudentId" : 102, ... "StudentDetails" : [ ... { ... "TheoryMarks": 91, ... "PracticalMarks": 91 ... }, ... { ... "TheoryMarks": 75, ... "PracticalMarks": 75 ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5c8edaf12f684a30fbdfd587") }
Display all documents from a collection with the help of find() method. The query is as follows −
> db.documentWithAParticularFieldValueDemo.find().pretty();
The following is the output −
{ "_id" : ObjectId("5c8edab72f684a30fbdfd586"), "StudentId" : 101, "StudentDetails" : [ { "TheoryMarks" : 78, "PracticalMarks" : 91 }, { "TheoryMarks" : 75, "PracticalMarks" : 75 } ] } { "_id" : ObjectId("5c8edaf12f684a30fbdfd587"), "StudentId" : 102, "StudentDetails" : [ { "TheoryMarks" : 91, "PracticalMarks" : 91 }, { "TheoryMarks" : 75, "PracticalMarks" : 75 } ] }
Here is the query to find documents with arrays not containing a document with a particular field value in MongoDB −
> db.documentWithAParticularFieldValueDemo.find({'StudentDetails.TheoryMarks': {$nin: [78]}}).pretty();
The following is the output −
{ "_id" : ObjectId("5c8edaf12f684a30fbdfd587"), "StudentId" : 102, "StudentDetails" : [ { "TheoryMarks" : 91, "PracticalMarks" : 91 }, { "TheoryMarks" : 75, "PracticalMarks" : 75 } ] }
- Related Articles
- MongoDB query to fetch a specific document rom documents with field value set using NumberInt()?
- Find the document by field name with a specific value in MongoDB?
- Find MongoDB document with array containing the maximum occurrence of a specific value
- Match MongoDB documents with fields not containing values in array?
- MongoDB query to fetch a document that does not have a particular field?
- MongoDB aggregation to fetch documents with specific field value?
- Find a value in lowercase from a MongoDB collection with documents
- Find documents which contain a particular value in MongoDB using Regular Expressions?
- Count the documents with a field value beginning with 13
- Find document with array that contains a specific value in MongoDB
- Find a document with ObjectID in MongoDB?
- Match MongoDB documents with field value greater than a specific number and fetch them?
- How do I find all documents with a field that is NaN in MongoDB?
- Search for documents with similar arrays in MongoDB and order by similarity value
- Check if value exists for a field in a MongoDB document?

Advertisements