
- 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 search for string or number in a field with MongoDB?
You can use $in operator for this. Let us first create a collection with documents −
> db.searchForStringOrNumberDemo.insertOne( { "_id": new ObjectId(), "StudentName": "Larry", "StudentDetails": { "StudentMarks": { "StudentMongoDBMarks": [44] } } } ); { "acknowledged" : true, "insertedId" : ObjectId("5ce2407c36e8b255a5eee944") } > db.searchForStringOrNumberDemo.insertOne( { "_id": new ObjectId(), "StudentName": "Larry", "StudentDetails": { "StudentMarks": { "StudentMongoDBMarks": ["44"] } } } ); { "acknowledged" : true, "insertedId" : ObjectId("5ce240f036e8b255a5eee945") }
Following is the query to display all documents from a collection with the help of find() method −
> db.searchForStringOrNumberDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5ce2407c36e8b255a5eee944"), "StudentName" : "Larry", "StudentDetails" : { "StudentMarks" : { "StudentMongoDBMarks" : [ 44 ] } } } { "_id" : ObjectId("5ce240f036e8b255a5eee945"), "StudentName" : "Larry", "StudentDetails" : { "StudentMarks" : { "StudentMongoDBMarks" : [ "44" ] } } }
Following is the query to search for string or number in a field −
>db.searchForStringOrNumberDemo.find({"StudentDetails.StudentMarks.StudentMongoDBMarks": { "$in": ["44",44] } });
This will produce the following output −
{ "_id" : ObjectId("5ce2407c36e8b255a5eee944"), "StudentName" : "Larry", "StudentDetails" : { "StudentMarks" : { "StudentMongoDBMarks" : [ 44 ] } } } { "_id" : ObjectId("5ce240f036e8b255a5eee945"), "StudentName" : "Larry", "StudentDetails" : { "StudentMarks" : { "StudentMongoDBMarks" : [ "44" ] } } }
- Related Articles
- MongoDB query to search for string like “@email” in the field values
- How to search for a record (field) and then delete it in MongoDB?
- Search a sub-field on MongoDB?
- Search a string with special characters in a MongoDB document?
- Search by specific field in MongoDB
- How to search for a date in MySQL timestamp field?
- Limit number of values in a field with MongoDB?
- Search a value in an object with number keys with MongoDB
- How to check if a field in MongoDB is [] or {}?
- How to search for a string in JavaScript?
- How to check if field is a number in MongoDB?
- How to query for records where field is null or not set in MongoDB?
- How to search for a pattern in a Java string?
- How to search a string for a pattern in JavaScript?
- Update all the values of a field with a specific string in MongoDB?

Advertisements