Ultimate Guide to Using the $in Operator in MongoDB

$In Operator Featured Image

In this article, I am going to discuss how we can use the $in operator in MongoDB.

MongoDB comes with a long list of operators for a variety of purposes. These operators could be used for a variety of purposes. For example, you might want to filter your queries in the database, or maybe perform an action inside your database. You might even want to compare between documents. Well, the list goes on if I were to sit and list down all the use cases.

Today, the $in operator in MongoDB is of our interest. The $in operator lets you find documents precisely. This operator can be used with the find() and update() methods as well.

The $in operator is used to select a document inside a collection. by comparing the values of a field that match the given value in the array. In case, the value of the field of a document is an array, the $in operator in MongoDB will select only those documents whose fields contain an array with at least one item matching the given value in them.

In this guide, I will explain to you how to use the $in operator for multiple uses. We will take a random database for our examples and collection with a bunch of documents in them.

Syntax of the $in Operator in MongoDB

{ field: { $in: [, , ...  ] }

So, let us get started with exploring and working with some of the use cases of the $in operator in MongoDB.

Using $in operator in MongoDB to Match Values

Let’s say we have a drone shop application and we have a database where we have stored all our drone items. Now, let us learn how we can extract documents whose results match based on a given value.

For this guide, I have created and saved some documents in my drones collection.

Follow the steps below to get started:

  • Start your MongoDB server
  • Pass this command to list all your databases and use your preferred one:
show dbs
use droneStore
  • Now, let us first list out all the documents in our collection. We can then use the $in operator on them to filter out based on specific values.
db.drones.find({})

{ "_id" : ObjectId("6034fd2bf74cfd0438bdb19b"), "utility" : [ "Photography" ], "onSale" : false, "name" : "SV - Aira 157B", "price" : 2455, "weight" : "467 grams", "__v" : 0 }

{ "_id" : ObjectId("603502ba24df1c2350e676e7"), "utility" : [ "Monitoring or Inspection", "Security" ], "onSale" : false, "name" : "SV - Terrus D40", "price" : 5259, "weight" : "764 grams", "__v" : 0 }

{ "_id" : ObjectId("603502ba24df1c2350e676e8"), "utility" : [ "Recreation" ], "onSale" : false, "name" : "SV - Jovio J259", "price" : 1500, "weight" : "247 grams", "__v" : 0 }

{ "_id" : ObjectId("603502ba24df1c2350e676e9"), "utility" : [ "Delivery" ], "onSale" : false, "name" : "SV - CARRIER X288", "price" : 105499, "weight" : "26 kilograms", "__v" : 0 }

{ "_id" : ObjectId("60352beadecee830681c3b62"), "utility" : [ "Delivery" ], "onSale" : false, "name" : "SV - Grander 345X8", "price" : 43535, "weight" : "23 kilograms", "__v" : 0 }

{ "_id" : ObjectId("60352ecb6ce2811cc8892a75"), "utility" : [ "Monitoring or Inspection" ], "onSale" : false, "name" : "SV - LaserX AW205", "price" : 4000, "weight" : "2.3 kilograms", "__v" : 0 }

{ "_id" : ObjectId("6157239d341d653bccaf7822"), "utility" : [ "Recreation" ], "onSale" : false, "name" : "DF - Jetfire Nitro RX-V", "price" : 22000, "weight" : "3 kilograms", "__v" : 0 }

{ "_id" : ObjectId("6157240c341d653bccaf7823"), "utility" : [ "Monitoring or Inspection" ], "onSale" : false, "name" : "Pink Sentinel Q95", "price" : 13000, "weight" : "2 kilograms", "__v" : 0 }

{ "_id" : ObjectId("61573885341d653bccaf7825"), "utility" : [ "Security" ], "onSale" : false, "name" : "SV - Xorvia 9908Y", "price" : 7760, "weight" : "3.8 kilograms", "__v" : 0 }

{ "_id" : ObjectId("615738d6341d653bccaf7826"), "utility" : [ "Photography" ], "onSale" : false, "name" : "SV - Ryee SW657", "price" : 8900, "weight" : "760 grams", "__v" : 0 }

{ "_id" : ObjectId("61573946341d653bccaf7827"), "utility" : [ "Monitoring or Inspection" ], "onSale" : false, "name" : "RV - Zereca Eagle-i", "price" : 3200, "weight" : "1.7 kilograms", "__v" : 0 }

{ "_id" : ObjectId("615739bb341d653bccaf7828"), "utility" : [ "Delivery" ], "onSale" : false, "name" : "OS - Falcon GO!", "price" : 45000, "weight" : "17 kilograms", "__v" : 0 }
  • Let us now use the $in operator to look for drones with the “Recreation” and “Security” as their utility.

As you can notice, we have four documents filtered out that contain “Recreation” and “Security” as their utility.

Updating Documents using $in Operator in MongoDB

Let us now use the update() method on our documents to update the values of our documents. Let us see how we can do so. Below are steps you can follow:

> db.drones.find({utility: {$in: ["Security", "Recreation"]}})

{ "_id" : ObjectId("603502ba24df1c2350e676e7"), "utility" : [ "Monitoring or Inspection", "Security" ], "onSale" : false, "name" : "SV - Terrus D40", "price" : 5259, "weight" : "764 grams", "__v" : 0 }

{ "_id" : ObjectId("603502ba24df1c2350e676e8"), "utility" : [ "Recreation" ], "onSale" : false, "name" : "SV - Jovio J259", "price" : 1500, "weight" : "247 grams", "__v" : 0 }

{ "_id" : ObjectId("6157239d341d653bccaf7822"), "utility" : [ "Recreation" ], "onSale" : false, "name" : "DF - Jetfire Nitro RX-V", "price" : 22000, "weight" : "3 kilograms", "__v" : 0 }

{ "_id" : ObjectId("61573885341d653bccaf7825"), "utility" : [ "Security" ], "onSale" : false, "name" : "SV - Xorvia 9908Y", "price" : 7760, "weight" : "3.8 kilograms", "__v" : 0 }
  • Let’s say we want to change the below drone:
{ "_id" : ObjectId("603502ba24df1c2350e676e8"), "utility" : [ "Recreation" ], "onSale" : false, "name" : "SV - Jovio J259", "price" : 1500, "weight" : "247 grams", "__v" : 0 }
  • Now, we shall pass this command to change the price to 2000 of this drone:
> db.drones.update({name: {$in: ["SV - Jovio J259"]}}, {$set: {price: 2000}})

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
  • Let us now check the document again using the $in operator although we could simply use the find() method here.
> db.drones.find({name: {$in: ["SV - Jovio J259"]}})

{ "_id" : ObjectId("603502ba24df1c2350e676e8"), "utility" : [ "Recreation" ], "onSale" : false, "name" : "SV - Jovio J259", "price" : 2000, "weight" : "247 grams", "__v" : 0 }

We have successfully used the $in operator in MongoDB.

Read More: Insert a Document into MongoDB Using Mongoose – Quick 2021 Guide

Conclusion

This guide discusses how you can use the $in operator in MongoDB to look up documents with a given filter and how can even update them.

Noteworthy References

MongoDB Docs