
- 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
MongoDB query to exclude if id is equal to a document field array value
For this, use $not along with $in. Let us create a collection with documents −
[ { id: "101", subjectid: [ "102" ] }, { id: "102", subjectid: [ "102" ] } ]
Here is the snapshot.
Display all documents from a collection with the help of find() method −
db.collection.find()
This will produce the following output −
[ { "_id": ObjectId("5a934e000102030405000000"), "id": "101", "subjectid": [ "102" ] }, { "_id": ObjectId("5a934e000102030405000001"), "id": "102", "subjectid": [ "102" ] } ]
Following is the query that uses $expr, $not and $in to fetch values excluding matching field array value −
db.collection.find({ $expr: { $not:{ $in: [ "$id", "$subjectid" ] } } })
This will produce the following output −
[ { "_id": ObjectId("5a934e000102030405000000"), "id": "101", "subjectid": [ "102" ] } ]
- Related Articles
- How to exclude array type field value in MongoDB?
- MongoDB query to test if a value is in array?
- MongoDB query to fetch a specific document rom documents with field value set using NumberInt()?
- MongoDB query to remove array elements from a document?
- Check if value exists for a field in a MongoDB document?
- MySQL UPDATE query where id is highest AND field is equal to variable?
- MongoDB query to pull a specific value from a document
- MongoDB query to push document into an array
- MongoDB query to fetch a document that does not have a particular field?
- MongoDB query to add new array element in document
- MongoDB query to update array with another field?
- MongoDB query (aggregation framework) to match a specific field value
- Add a field to an embedded document in an array in MongoDB?
- MongoDB query to remove a specific document
- How to add a field with static value to MongoDB find query?

Advertisements