
- 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 add matched key to list after query?
For this, use aggregate() in MongoDB. Let us create a collection with documents −
> db.demo334.insertOne({ ... "Name": "Chris", ... "Age": 21, ... "details": [{ ... "Subject": "MySQL", ... "Score": 78 ... }, { ... "Subject": "MongoDB", ... "Score": 45 ... }] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e52241bf8647eb59e562090") }
Display all documents from a collection with the help of find() method −
> db.demo334.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5e52241bf8647eb59e562090"), "Name" : "Chris", "Age" : 21, "details" : [ { "Subject" : "MySQL", "Score" : 78 }, { "Subject" : "MongoDB", "Score" : 45 } ] }
Following is the query to add matched key to list after query −
> db.demo334.aggregate([ ... { $addFields: { ... details: {$filter: { ... input: '$details', ... as: 'out', ... cond: {$gte: ['$$out.Score',70 ]} ... }} ... }} ... ])
This will produce the following output −
{ "_id" : ObjectId("5e52241bf8647eb59e562090"), "Name" : "Chris", "Age" : 21, "details" : [ { "Subject" : "MySQL", "Score" : 78 } ] }
- Related Articles
- MongoDB query to add multiple documents
- MongoDB Query to select records having a given key?
- How to query on list field in MongoDB?
- MongoDB query to sum specific key values with terminal commands?
- MongoDB query to add new array element in document
- How to query a key having space in its name with MongoDB?
- How to add a field with static value to MongoDB find query?
- MongoDB query to set user defined variable into query?
- MongoDB query to add a document in an already created collection
- MongoDB query to add timestamp only if it is not present
- MongoDB aggregate query to sort
- MongoDB query to calculate average
- MongoDB query to skip documents
- MongoDB query to sort subdocuments
- MongoDB query to update tag

Advertisements