
- 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
Extract a MongoDB document with a specific string
To extract a MongoDB document with a specific string, use $match in MongoDB. Let us create a collection with documents −
> db.demo424.insert( ... { ... ... "Information" : [ ... { ... id:10, ... Name:"Chris" ... }, ... { ... id:11, ... Name:"David" ... } ... ] ... } ... ) WriteResult({ "nInserted" : 1 }) > > db.demo424.insert( ... { ... ... "Information" : [ ... { ... id:101, ... Name:"Mike" ... }, ... { ... id:102, ... Name:"John" ... } ... ] ... } ... ) WriteResult({ "nInserted" : 1 })
Display all documents from a collection with the help of find() method −
> db.demo424.find();
This will produce the following output −
{ "_id" : ObjectId("5e74eb9dbbc41e36cc3cae6a"), "Information" : [ { "id" : 10, "Name" : "Chris" }, { "id" : 11, "Name" : "David" } ] } { "_id" : ObjectId("5e74eb9ebbc41e36cc3cae6b"), "Information" : [ { "id" : 101, "Name" : "Mike" }, { "id" : 102, "Name" : "John" } ] }
Following is the query to extract a MongoDB document with a specific string −
> db.demo424.aggregate( ... [ { $match : { "Information.Name": "Chris" } } ] ... );
This will produce the following output −
{ "_id" : ObjectId("5e74eb9dbbc41e36cc3cae6a"), "Information" : [ { "id" : 10, "Name" : "Chris" }, { "id" : 11, "Name" : "David" } ] }
- Related Articles
- Find which MongoDB document contains a specific string?
- Fetch a specific document in MongoDB with array elements
- MongoDB query to remove a specific document
- Filter specific values from a MongoDB document
- Search a string with special characters in a MongoDB document?
- Find document with array that contains a specific value in MongoDB
- Update only a specific value in a MongoDB document
- Find the document by field name with a specific value in MongoDB?
- Update a specific MongoDB document in array with $set and positional $ operator?
- Return specific MongoDB embedded document
- MongoDB query to update a specific document from a collection
- MongoDB query to pull a specific value from a document
- How to find MongoDB documents with a specific string?
- Find MongoDB document with array containing the maximum occurrence of a specific value
- Accessing array values in a MongoDB collection to fetch a specific document

Advertisements