
- 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 get data of array intersection in MongoDB?
For array interection in MongoDB, use the $setIntersection in aggregate(). Let us create a collection with documents −
> db.demo625.insertOne( ... { ... Name: "John", ... Marks: [56,98,60] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e9ab8e16c954c74be91e6aa") } > db.demo625.insertOne( ... { ... Name: "John", ... Marks: [110,56,72] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e9ab8e26c954c74be91e6ab") } > db.demo625.insertOne( ... { ... Name: "Chris", ... Marks: [90,91] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e9ab8e26c954c74be91e6ac") } > db.demo625.insertOne( ... { ... Name: "Robert", ... Marks: [60,75] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e9ab8e26c954c74be91e6ad") }
Display all documents from a collection with the help of find() method −
> db.demo625.find();
This will produce the following output −
{ "_id" : ObjectId("5e9ab8e16c954c74be91e6aa"), "Name" : "John", "Marks" : [ 56, 98, 60 ] } { "_id" : ObjectId("5e9ab8e26c954c74be91e6ab"), "Name" : "John", "Marks" : [ 110, 56, 72 ] } { "_id" : ObjectId("5e9ab8e26c954c74be91e6ac"), "Name" : "Chris", "Marks" : [ 90, 91 ] } { "_id" : ObjectId("5e9ab8e26c954c74be91e6ad"), "Name" : "Robert", "Marks" : [ 60, 75 ] }
Following is the query to get data of array intersection in MongoDB −
> db.demo625.aggregate([ ... ... ... { "$project": { ... "Name": 1, ... "Marks": { ... "$setIntersection": [ "$Marks", [56,98,60] ] ... } ... }} ... ])
This will produce the following output −
{ "_id" : ObjectId("5e9ab8e16c954c74be91e6aa"), "Name" : "John", "Marks" : [ 56, 60, 98 ] } { "_id" : ObjectId("5e9ab8e26c954c74be91e6ab"), "Name" : "John", "Marks" : [ 56 ] } { "_id" : ObjectId("5e9ab8e26c954c74be91e6ac"), "Name" : "Chris", "Marks" : [ ] } { "_id" : ObjectId("5e9ab8e26c954c74be91e6ad"), "Name" : "Robert", "Marks" : [ 60 ] }
- Related Articles
- How to get the intersection of two arrays in MongoDB?
- How to get embedded data in a MongoDB document?
- How to get specific data in different formats with MongoDB?
- How to get array from a MongoDB collection?
- How to get items from an object array in MongoDB?
- How to use $slice operator to get last element of array in MongoDB?
- Get the array of _id in MongoDB?
- How to only get the data of the nested JSON object in MongoDB?
- Get database data size in MongoDB?
- MongoDB query to get array of nested string?
- How to get a particular element from MongoDB array?
- MongoDB query to get average in aggregation of array element?
- How to get the matching document inside an array in MongoDB?
- Get distinct levels of array field in MongoDB?
- How to get the index of an array element in older versions on MongoDB?

Advertisements