
- 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
Filter specific values from a MongoDB document
To filter specific values, use $filter in MongoDB. Let us create a collection with documents −
> db.demo751.insertOne( ... { ... _id: 101, ... details: [ ... { Name: "Robert", id:110,Age:21}, ... { Name: "Rae", id:110,Age:22}, ... {Name: "Ralph", id:116,Age:23} ... ] ... } ... ); { "acknowledged" : true, "insertedId" : 101 }
Display all documents from a collection with the help of find() method −
> db.demo751.find().pretty();
This will produce the following output −
{ "_id" : 101, "details" : [ { "Name" : "Robert", "id" : 110, "Age" : 21 }, { "Name" : "Rae", "id" : 110, "Age" : 22 }, { "Name" : "Ralph", "id" : 116, "Age" : 23 } ] }
Following is the query to filter −
> db.demo751.aggregate([ ... { ... $addFields: { ... details: { ... $let: { ... vars: { ... filtered: { $filter: { input: "$details", as: "out", cond: { $eq: [ "$$out.id", 110 ] } } } ... }, ... in: { $slice: [ "$$filtered", -1 ] } ... } ... } ... } ... } ... ])
This will produce the following output −
{ "_id" : 101, "details" : [ { "Name" : "Rae", "id" : 110, "Age" : 22 } ] }
- Related Articles
- MongoDB query to update a specific document from a collection
- MongoDB query to pull a specific value from a document
- Accessing array values in a MongoDB collection to fetch a specific document
- Remove values from a matrix like document in MongoDB
- How to get a specific object from array of objects inside specific MongoDB document?
- Return specific MongoDB embedded document
- MongoDB query to remove a specific document
- How to find specific array elements in MongoDB document with query and filter with range?
- Extract a MongoDB document with a specific string
- MongoDB query select and display only a specific field from the document?
- Filter sub documents by sub document in MongoDB?
- Find which MongoDB document contains a specific string?
- Update only a specific value in a MongoDB document
- How to project specific fields from a document inside an array in Mongodb?
- How to subtract values (TotalPrice – Discount) from document field values in MongoDB?

Advertisements