
- 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
Match MongoDB documents with fields not containing values in array?
To match documents with fields not containing values in array, use $nin. Let us create a collection with documents −
> db.demo180.insertOne({"Scores":["80","90","110"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e3988a69e4f06af551997fb") } > db.demo180.insertOne({"Scores":["110","70","60"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e3988b79e4f06af551997fc") } > db.demo180.insertOne({"Scores":["40","70","1010"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e3988cc9e4f06af551997fd") }
Display all documents from a collection with the help of find() method −
> db.demo180.find();
This will produce the following output −
{ "_id" : ObjectId("5e3988a69e4f06af551997fb"), "Scores" : [ "80", "90", "110" ] } { "_id" : ObjectId("5e3988b79e4f06af551997fc"), "Scores" : [ "110", "70", "60" ] } { "_id" : ObjectId("5e3988cc9e4f06af551997fd"), "Scores" : [ "40", "70", "1010" ] }
Following is the query to match documents with fields not containing values in array −
> db.demo180.aggregate({ "$match": { "Scores": { "$nin": ["110","90"] } } }).pretty();
This will produce the following output −
{ "_id" : ObjectId("5e3988cc9e4f06af551997fd"), "Scores" : [ "40", "70", "1010" ] }
- Related Articles
- MongoDB query to match documents with array values greater than a specific value
- Get fields from multiple sub-documents that match a condition in MongoDB?
- Group all documents with common fields in MongoDB?
- How do I work with array fields in MongoDB to match all?
- Match ID and fetch documents with $eq in MongoDB in case of array?
- Find documents with arrays not containing a document with a particular field value in MongoDB?
- MongoDB query to concatenate values of array with other fields
- MongoDB query to get only specific fields in nested array documents?
- Count unique items in array-based fields across all MongoDB documents?
- MongoDB query to match documents that contain an array field
- MongoDB query to find matching documents given an array with values?
- Match between fields in MongoDB aggregation framework?
- How to find exact Array Match with values in different order using MongoDB?
- MongoDB query to match each element in a documents array to a condition?
- Select two fields and return a sorted array with their distinct values in MongoDB?

Advertisements