JIYIK CN >

Current Location:Home > Learning > DATABASE > MongoDB >

The in operator in MongoDB

Author:JIYIK Last Updated:2025/04/10 Views:

This MongoDB article will discuss the use of the $in operator in MongoDB.


IN Operator ($in) in MongoDB

MongoDB comes with a number of operators for a range of tasks. The $in operator allows you to precisely locate a document, find()and update()the methods can be used with this operator.

The $in operator compares the value of a field with the values ​​provided in an array to select documents in a collection. If the value of a document field is an array, MongoDB's $in operator will select only those documents where the field contains an array and at least one item matches the provided value.

grammar:

{ field: { $in: [<value1>, <value2>, ... <valuen> ] }

Using the $in operator to match values ​​in MongoDB

Suppose you have a fruit store database management system, and you have a database where you store all the data of your fruit items, such as price, quantity, weight, etc. We will discuss below how to extract the documents that match the result based on a given value.

First, start your MongoDB server and pass the following command to list all databases and use your database.

Order:

show dbs
use data

Let's list all the documents in the collection you have. You can then use the $in operator on them to filter out based on a specific value.

db={
  "data": [
    {
      "_id": ObjectId("6034fd2bf74cfd0438bdb19b"),
      "onSale": false,
      "name": "Orange",
      "price": 500,
      "weight": "1 kilograms",
      "__v": 0
    },
    {
      "_id": ObjectId("603502ba24df1c2350e676e7"),
      "onSale": false,
      "name": "Banana",
      "price": 350,
      "weight": "2 dozen",
      "__v": 0
    },
    {
      "_id": ObjectId("603502ba24df1c2350e676e8"),
      "onSale": false,
      "name": "Apple",
      "price": 125,
      "weight": "500 grams",
      "__v": 0
    },
    {
      "_id": ObjectId("603502ba24df1c2350e676e9"),
      "onSale": false,
      "name": "Mango",
      "price": 300,
      "weight": "4 kilograms",
      "__v": 0
    }
  ]
}

Updating Documents in MongoDB Using $in Operator

Use the method on a document update()to update the value of certain documents.

db={
  "data": [
    {
      "_id": ObjectId("6034fd2bf74cfd0438bdb19b"),
      "onSale": false,
      "name": "Orange",
      "price": 500,
      "weight": "1 kilograms",
      "__v": 0
    },
    {
      "_id": ObjectId("603502ba24df1c2350e676e7"),
      "onSale": false,
      "name": "Banana",
      "price": 350,
      "weight": "2 dozen",
      "__v": 0
    },
    {
      "_id": ObjectId("603502ba24df1c2350e676e8"),
      "onSale": false,
      "name": "Apple",
      "price": 125,
      "weight": "500 grams",
      "__v": 0
    },
    {
      "_id": ObjectId("603502ba24df1c2350e676e9"),
      "onSale": false,
      "name": "Mango",
      "price": 300,
      "weight": "4 kilograms",
      "__v": 0
    }
  ]
}

If you want to change the price of the fruit "Apple". You can change the price of "Apple" to "200" through this query.

Query:

db.data.update({
  name: {
    $in: [
      "Apple"
    ]
  }
},
{
  $set: {
    price: 200
  }
})

After using find()the method, you will get the following result. update()The price of "Apple" is updated from 125 to 200 using the $in operator in the method.

Output:

Use the $in operator to update values ​​in the database

Through this MongoDB tutorial article, you have learned what the $in operator is, how to use the $in operator to match values ​​in a database collection, and how to update or edit a given collection in the database.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

$ne operator in MongoDB

Publish Date:2025/04/11 Views:84 Category:MongoDB

This article will discuss how the $ne operator works in MongoDB. In addition, we will list its differences from the $not operator. $ne operator in MongoDB $ne is an operator in MongoDB that stands for not equal to. This will compare the val

MongoDB $Set Operator

Publish Date:2025/04/11 Views:159 Category:MongoDB

With the help of this article, you will learn how to use $set the operator to partially update objects in MongoDB so that the new object overlaps/merges with the existing object. The $set operator replaces the value of a field with a given

Difference between $push and $addToSet in MongoDB

Publish Date:2025/04/11 Views:63 Category:MongoDB

This article explains the operators in MongoDB. What is the purpose of $push and $addToSet operators. Furthermore, the difference between these two operators is given in the code snippet. This article discusses the following topics. Operato

Sort a collection by date in MongoDB

Publish Date:2025/04/11 Views:64 Category:MongoDB

In this MongoDB tutorial, the problem of sorting a collection in MongoDB is discussed. The different ways to sort a collection in the database are briefly explained. Using sort() function in MongoDB This problem is solved using the MongoDB

Counting records in MongoDB

Publish Date:2025/04/11 Views:146 Category:MongoDB

This article discusses operators in MongoDB, aggregation operators, and different ways to calculate the total number of records. Operations in MongoDB CRUD operations are a user interface concept that allows users to browse, search, and cha

Pretty printing in MongoDB

Publish Date:2025/04/11 Views:150 Category:MongoDB

This article will discuss how to use pretty printing in MongoDB to display formatted results. Pretty printing in MongoDB A cursor is an object that allows programmers in the Mongo world to iterate over documents in a Mongo collection. Altho

MongoDB Adding Elements to an Array

Publish Date:2025/04/11 Views:136 Category:MongoDB

This article will cover the various ways to add to an array in MongoDB. Adding to an array in MongoDB Use the $push operator to add values ​​to an array The $push operator is one of the various array update operators provided by MongoDB

MongoDB Search by ID

Publish Date:2025/04/11 Views:131 Category:MongoDB

The following article provides an overview of MongoDB find by Id() method. MongoDB provides a find by Id() function which can retrieve documents matching a user id. To use search by Id() in MongoDB, you need to use the find() function. If n

MongoDB starts with a query

Publish Date:2025/04/10 Views:195 Category:MongoDB

In this MongoDB article, users will learn how to start queries using $regex. It provides regular expression functionality for pattern matching strings in queries. MongoDB starts querying using $regex If you want to use $regex , use one of t

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial