EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials MongoDB Tutorial MongoDB Not In
 

MongoDB Not In

Updated March 8, 2023

MongoDB-Not-In

 

 

Definition of MongoDB Not In

MongoDB not in is comparison operator, not in operator is basically used to select the documents which value is not matching to the given value. We can also use not in operator on array field, using array field not in operator is selecting the array fields which array elements does not exist in our selection criteria. We have using not in operator with $ sign in MongoDB, its abbreviation form to use in MongoDB is $nin. We can use not in operator with find method to select the documents and with update method to update the documents.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Syntax

Below is the syntax of not in operator in MongoDB.

1) MongoDB not in operator with find method –

db.name_of_collection.find ( { field_name: { $nin: [ "value1", "value2" ] } } ).pretty ()

2) MongoDB not in operator with update method –

db.name_of_collection.update ( { field_name: {$nin: ["value1", "value2" ] } }, { $set: { field_name: “value” } } )

Parameter description syntax of not in operator in MongoDB.

1) Name of collection – This parameter is defined as the name of the collection which was used in select and update the document which was not matching to the given selection criteria.

2) Find – This method is used with not in operator to select those documents which were not matching our selection criteria.

3) Field name – This is a field of collection that was used with not in operator to update or select the non-matching documents from our selection criteria which were we have used in the query.

4) Not in – This operator is used in MongoDB to retrieve or update the documents from the collection which was not meet our selection criteria.

5) Value – This is defined as the value of the field which we have updating using not in operator in MongoDB. If we want to search any document in MongoDB we can search using field value.

6) Update – This method is used with not in operator to update those documents which were not matching our selection criteria.

7) Pretty – Basically MongoDB output is in an unstructured format using the find and update method. While using the pretty method we can display document output in a structured format.

8) Set – This is an important parameter while doing update operations on documents in MongoDB. We have using a set operator with a not in operator in MongoDB to update the non-matching documents from the given criteria.

How not in works in MongoDB?

MongoDB not in operator is basically used to retrieve the document which was not in our selection criteria. We can also retrieve the array fields using not in condition. Array field retrieves as a per-element basis, those array documents result we have not required we need to put that element in selection criteria.

Not in operator will returns empty result when we have found all the matching documents in our selection criteria.

In the below example, we can see that after using not in operator on remove_field collection it will give empty result, because we have used array elements as 55 and 60 which was present in all the documents so it will return an empty result.

Code:

db.remove_field.find ( { marks: { $nin: [ 55, 60 ] } } )
db.remove_field.find ()

1

Figure – not in operator will return an empty result when we have found all the matching documents in our selection criteria.

Not in operator will not update any documents when we have found all the matching documents from the collection in our selection criteria.

In below example, we can see that after using not in operator on query_array collection it will not update any document, because we have used array element as 50 and 70 which was present in all the documents so it will not update any documents from the collection.

Code:

db.query_array.update ( { marks: { $nin: [ 50, 70 ] } }, { $set: { name: "MNP" } } )
db.query_array.find ()

4

Figure – not in operator will not update any documents when we have found all the matching documents from the collection.

Example

The below example shows not in operator in MongoDB.

1) MongoDB not in operator using matching values –

The below example shows that not in operators with matching values are as follows. We have finding the student name whose name was not in “ABC”.

We have to find the student name using not in operator from query_array collection.

Code:

db.query_array.find ( { stud_name: { $nin: [ "ABC" ] } } )
db.query_array.find ()

MongoDB not in 1

Figure – Example of MongoDB not in operator using matching values.

2) MongoDB not in operator using array fields –

The below example shows that not in operator with array fields. We have to find the student marks which marks not in 85 and 95.

We have to find the student marks using not in operator from query_array collection.

Code:

db.query_array.find ( { stud_name: { $nin: [ "ABC" ] } } )
db.query_array.find ()

MongoDB not in 2

Figure – Example of MongoDB not in operator using array fields.

3) MongoDB not in operator using embedded documents –

The below example shows that not in operator with embedded documents. We have found the name which lap_spec.ram not in 8 and 16.

We have finding the name using not in operator from MongoDB_Notin collection.

Code:

db.MongoDB_Notin.find ( { "lap_spec.ram": { $nin: [ 8, 16 ] } } )
db.MongoDB_Notin.find ()

MongoDB not in 3

Figure – Example of MongoDB not in operator using embedded documents.

4) Update the documents using not in operator–

Below example shows that update the documents using not in operator. We have updated the documents which name not in “ABC”, “XYZ” and “MNP”.

We have updated the name using not in operator from MongoDB_Notin collection.

Code:

db.MongoDB_Notin.update ( { name: { $nin: [ "ABC", "XYZ", "MNP" ] } }, { $set: { name: "BCD" } } )
db.MongoDB_Notin.find ()

3

Figure – Example to update the documents using not in operator.

Conclusion

Not in operator is used to select documents from a collection that was not matching our selection criteria. While using not in operator in MongoDB we need to use prefix as “$” sign. The value list of not in operator can contain the array fields, embedded documents, literal and regular expressions.

Recommended Articles

This is a guide to MongoDB Not In. Here we discuss the definition, syntax, How not in works in MongoDB? examples with code implementation. You may also have a look at the following articles to learn more –

  1. MongoDB Delete Collection
  2. MongoDB group by
  3. MongoDB Database
  4. MongoDB Import

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW