
- 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
MongoDB query to collectively match intersection of documents against a field
For this, use aggregate(). Let us first create a collection with documents −
> db.demo393.insertOne( ... { ... Id1: "1", ... Name: "Chris", ... Id2: "100" ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e5e6dd522064be7ab44e804") } > db.demo393.insertOne( ... { ... Id1: "1", ... Name: "Chris", ... Id2: "101" ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e5e6dd522064be7ab44e805") } > db.demo393.insertOne( ... { ... Id1: "3", ... Name: "Chris", ... Id2: "100" ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e5e6dd522064be7ab44e806") } > db.demo393.insertOne( ... { ... Id1: "3", ... Name: "Mike", ... Id2: "101" ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e5e6dd522064be7ab44e807") }
Display all documents from a collection with the help of find() method −
> db.demo393.find();
This will produce the following output −
{ "_id" : ObjectId("5e5e6dd522064be7ab44e804"), "Id1" : "1", "Name" : "Chris", "Id2" : "100" } { "_id" : ObjectId("5e5e6dd522064be7ab44e805"), "Id1" : "1", "Name" : "Chris", "Id2" : "101" } { "_id" : ObjectId("5e5e6dd522064be7ab44e806"), "Id1" : "3", "Name" : "Chris", "Id2" : "100" } { "_id" : ObjectId("5e5e6dd522064be7ab44e807"), "Id1" : "3", "Name" : "Mike", "Id2" : "101" }
Following is the query to collectively match intersection of documents against a field −
> db.demo393.aggregate([ ... { "$match": { "Name": "Chris" } }, ... { "$group": { ... "_id": "$Id1", ... "docs": { "$push": "$$ROOT" }, ... "count": { "$sum": 1 } ... }}, ... { "$match": { ... "count": { "$gt": 1 }, ... "docs": { ... "$all": [ ... { "$elemMatch": { "Id2": "100" } }, ... { "$elemMatch": { "Id2": "101" } } ... ] ... } ... }} ... ])
This will produce the following output −
{ "_id" : "1", "docs" : [ { "_id" : ObjectId("5e5e6dd522064be7ab44e804"), "Id1" : "1", "Name" : "Chris", "Id2" : "100" }, { "_id" : ObjectId("5e5e6dd522064be7ab44e805"), "Id1" : "1", "Name" : "Chris", "Id2" : "101" } ], "count" : 2 }
- Related Articles
- MongoDB query to match documents that contain an array field
- MongoDB query (aggregation framework) to match a specific field value
- MongoDB query to match each element in a documents array to a condition?
- MongoDB query to update each field of documents in collection with a formula?
- MongoDB query to add up the values of a specific field in documents
- MongoDB query to match documents with array values greater than a specific value
- MongoDB query to match documents whose _id is in an array as part of a subdocument?
- Match MongoDB documents with field value greater than a specific number and fetch them?
- MongoDB query to skip documents
- MongoDB - Query embedded documents?
- MongoDB query to fetch a specific document rom documents with field value set using NumberInt()?
- MongoDB query to add multiple documents
- MongoDB query to group duplicate documents
- MongoDB query to count the number of array items in documents and display in a new field
- Sum MongoDB Sub-documents field?

Advertisements