
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
How to search an array for values present in another array and output the indexes of values found into a new array in MongoDB?
For this, use $indexOfArray. Let us first create a collection with documents −
> db.demo381.insertOne({"Values":[10,40,60,30,60]}); { "acknowledged" : true, "insertedId" : ObjectId("5e5b59f72ae06a1609a00b15") } > db.demo381.insertOne({"Values":[100,500,700,500,800]}); { "acknowledged" : true, "insertedId" : ObjectId("5e5b59f72ae06a1609a00b16") } > db.demo381.insertOne({"Values":[20,40,30,10,60]}); { "acknowledged" : true, "insertedId" : ObjectId("5e5b59f72ae06a1609a00b17") }
Display all documents from a collection with the help of find() method −
> db.demo381.find();
This will produce the following output −
{ "_id" : ObjectId("5e5b59f72ae06a1609a00b15"), "Values" : [ 10, 40, 60, 30, 60 ] } { "_id" : ObjectId("5e5b59f72ae06a1609a00b16"), "Values" : [ 100, 500, 700, 500, 800 ] } { "_id" : ObjectId("5e5b59f72ae06a1609a00b17"), "Values" : [ 20, 40, 30, 10, 60 ] }
Following is the query to search an array for values present in another array and output the indexes of values found into a new array in MongoDB −
> db.demo381.aggregate([ ... {"$project":{ ... "Result":{ ... "$map":{ ... "input":[10,40], ... "in":{"$indexOfArray":["$Values","$$this"]} ... } ... } ... }} ... ])
This will produce the following output −
{ "_id" : ObjectId("5e5b59f72ae06a1609a00b15"), "Result" : [ 0, 1 ] } { "_id" : ObjectId("5e5b59f72ae06a1609a00b16"), "Result" : [ -1, -1 ] } { "_id" : ObjectId("5e5b59f72ae06a1609a00b17"), "Result" : [ 3, 1 ] }
- Related Articles
- Return indexes of greatest values in an array in JavaScript
- Mapping an array to a new array with default values in JavaScript
- Min and max values of an array in MongoDB?
- How to store array values in MongoDB?
- C++ program to search specific values in an array
- Make an array of another array's duplicate values in JavaScript
- Get values that are not present in another array in JavaScript
- Clip (limit) the values in an array and place the result in another array in Numpy
- How to copy a section of an array into another array in C#?
- C++ Permutation of an Array that has Smaller Values from Another Array
- Search an array of hashes in MongoDB?
- How to read a CSV file and store the values into an array in C#?
- Get the length of distinct values in an array with MongoDB
- PHP Pushing values into an associative array?
- Looping numbers with object values and push output to an array - JavaScript?

Advertisements