MongoDB – $nin Operator

MongoDB provides different types of comparison query operators and $nin (not in) operator is one of them. This operator is used to select those documents where the value of the field is not equal to any of the given value in the array and the field that does not exist.
If the field contain an array, then this operator selects only those documents whose field contains an array that holds no item that matches a value of the specified array. You can use this operator in methods like find(), update(), etc. according to your requirements.

Syntax:

{field: {$nin: [value1, value2, value3, ...]}}

In the following examples, we are working with:

Database: GeeksforGeeks
Collection: contributor
Document: three documents that contain the details of the contributors in the form of field-value pairs.



Matching values using $nin operator:

In this example, we are retrieving only those employee’s documents whose name is not Amit or Suman.

filter_none

edit
close

play_arrow

link
brightness_4
code

db.contributor.find({name: {$nin: ["Amit", "Suman"]}}).pretty()

chevron_right


Matching values in an array using $nin operator:

In this example, we are retrieving only those employee’s documents who is not working with either C#, Python, or both languages.

filter_none

edit
close

play_arrow

link
brightness_4
code

db.contributor.find({language: {$nin: ["C#", "Python"]}}).pretty()

chevron_right


Matching values in embedded/nested documents using $nin operator:

In this example, we are retrieving only those employee’s documents whose age is not 25 or 22.

filter_none

edit
close

play_arrow

link
brightness_4
code

db.contributor.find({"personal.age": { $nin: [25, 22]}}).pretty()

chevron_right


Updating data using $nin operator:

In this example, we are adding a new field-value pair(i.e, salary: 10000) in the documents of those employee’s whose name is not Amit or Priya by using update() method with $in and $set operators.

filter_none

edit
close

play_arrow

link
brightness_4
code

db.contributor.update({name: {$nin: ["Amit", "Priya"]}},
                             {$set: {salary: 1000}})

chevron_right


Note: The update() method by default update only one document at a time. If you want to update multiple documents, then set the value of its multi parameter to true. So, in this example, the update() method updated the first document that matches the given condition as shown in the below image.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.