JIYIK CN >

Current Location:Home > Learning > DATABASE > MongoDB >

Search in a specified array in MongoDB

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

$inOperator searches within an array that you provide. For example, if you are given an array and want to search for one or more matching elements in a MongoDB collection, you can use $in.

Use $inthe operator to search for a specified array in MongoDB

$inThe operator finds documents where a field value is equal to any of the values ​​in the provided array. Use the following prototype to specify $inan expression.

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

For example, suppose you have the following collection.

{
    _id:1,
    name: "John",
    age: 30
},
{
    _id:2,
    name: "Alex",
    age: 23
},
{
    _id:3,
    name: "Murphy",
    age: 25
},
{
    _id:4,
    name: "Alice",
    age: 28
},

Now, you want to view _id = 2,3,4the documentation for . Therefore, the command will be as follows.

db.collection.find( {_id: { $in: [2, 3, 4] } } );

Alternatively, you can also send an array as a parameter. It can be useful when you are developing an application or a website.

let list=[2, 3, 4]
db.collection.find( { _id:{ $in: list} } )

promisesThis approach is useful when working with JavaScript .

To avoid errors, you should pay attention to the ObjectId.Default _idprovided by MongoDB, _idwhich is of type ObjectId.

Therefore, when passing an array into $inthe argument of , it is best to convert each integer idto ObjectId, as shown below.

{ "_id" : { $in : [ObjectId('2'),ObjectId('3'),ObjectId('4')] } }

This method is more accurate because you provide _idthe exact data type of the through the parameter. It can $inalso be used for updateMany,,,, operations.updateOnedeleteOnedeleteMany

To learn $inmore about the operator, visit the official page here .

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

List all collections in MongoDB Shell

Publish Date:2025/04/29 Views:156 Category:MongoDB

When using MongoDB , there are several ways to list the collections in the database. This article will discuss four different ways to get a list of collections in a MongoDB database. These methods are as follows: show collections List all c

Querying for non-null values in MongoDB

Publish Date:2025/04/29 Views:139 Category:MongoDB

This MongoDB article will explain how to query for non-null values ​​in MongoDB. To query for non-null values, you can use $ne the operator and $eq the operator and then specify the desired value to query. This article shows the readers

Shutting down with code:100 error in MongoDB

Publish Date:2025/04/29 Views:53 Category:MongoDB

This MongoDB tutorial will teach you to fix the error on different operating systems shutting down with code:100 . It will also talk about the root cause of why this issue occurs. shutting down with code:100 Errors in MongoDB As we all know

SELECT COUNT GROUP BY in MongoDB

Publish Date:2025/04/29 Views:74 Category:MongoDB

In this article, we will discuss the functions in MongoDB. Also, we will point out the aggregation functions in detail. We will explain in detail the different ways to count and sort multiple and single fields of Group in MongoDB. Operation

Differences between MongoDB and Mongoose

Publish Date:2025/04/29 Views:80 Category:MongoDB

This MongoDB article will discuss the differences between MongoDB and Mongoose. Unfortunately, most beginners tend to confuse these two concepts when they start developing applications and use MongoDB as their backend. MongoDB has its own s

Install MongoDB using Homebrew

Publish Date:2025/04/29 Views:161 Category:MongoDB

MongoDB is a well-known unstructured database management system that can handle large amounts of data. It is a document-oriented database system and belongs to the NoSQL family (non-SQL). Data and records are stored as documents that look a

Create a MongoDB dump of the database

Publish Date:2025/04/29 Views:62 Category:MongoDB

In this MongoDB article, you’ll get a walkthrough of Mongodump and Mongorestore , how to use them, and some simple examples of backing up and restoring your collections using both tools. mongodump Commands in MongoDB Mongodump is a tool t

Get the size of the database in MongoDB

Publish Date:2025/04/29 Views:88 Category:MongoDB

When working in MongoDB, do you know the size of your database? Today, we will learn how to get the size of a database in MongoDB using show dbs the command and the method. db.stats() Get the size of the database in MongoDB We can show dbs;

Grouping values by multiple fields with MongoDB

Publish Date:2025/04/29 Views:99 Category:MongoDB

MongoDB Group by Multiple Fields is used to group values ​​by multiple fields using various methods. One of the most efficient ways to group various fields present in MongoDB documents is by using $group the operator, which helps in per

Scan to Read All Tech Tutorials

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

Recommended

Tags

Scan the Code
Easier Access Tutorial