- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find values with OR operator in MongoDB and format the result.?
Use $or operator to fetch values and to format the result, use “pretty()”. Let us create a collection with documents −
> db.demo304.insertOne({"StudentName":"Chris","StudentAge":23}); { "acknowledged" : true, "insertedId" : ObjectId("5e4ea3ccf8647eb59e562034") } > db.demo304.insertOne({"StudentName":"David","StudentAge":22}); { "acknowledged" : true, "insertedId" : ObjectId("5e4ea3d7f8647eb59e562035") } > db.demo304.insertOne({"StudentName":"Mike","StudentAge":24}); { "acknowledged" : true, "insertedId" : ObjectId("5e4ea3e2f8647eb59e562036") } > db.demo304.insertOne({"StudentName":"Carol","StudentAge":22}); { "acknowledged" : true, "insertedId" : ObjectId("5e4ea3eef8647eb59e562037") }
Display all documents from a collection with the help of find() method −
> db.demo304.find();
This will produce the following output −
{ "_id" : ObjectId("5e4ea3ccf8647eb59e562034"), "StudentName" : "Chris", "StudentAge" : 23 } { "_id" : ObjectId("5e4ea3d7f8647eb59e562035"), "StudentName" : "David", "StudentAge" : 22 } { "_id" : ObjectId("5e4ea3e2f8647eb59e562036"), "StudentName" : "Mike", "StudentAge" : 24 } { "_id" : ObjectId("5e4ea3eef8647eb59e562037"), "StudentName" : "Carol", "StudentAge" : 22 }
Following is the query to get values with OR operator in MongoDB −
> db.demo304.find({ $or: [{ "StudentName": "David" }, { "StudentAge":22} ] } ).pretty();
This will produce the following output −
{ "_id" : ObjectId("5e4ea3d7f8647eb59e562035"), "StudentName" : "David", "StudentAge" : 22 } { "_id" : ObjectId("5e4ea3eef8647eb59e562037"), "StudentName" : "Carol", "StudentAge" : 22 }
- Related Articles
- Select multiple values with MongoDB OR operator
- MongoDB query to implement OR operator in find()
- MongoDB to fetch documents with $or Operator
- Get the aggregated result and find the count of repeated values in different MongoDB\ndocuments
- Combine SUM and FORMAT in MySQL to format the result
- Find in MongoDB documents with filled nested array and reshape the documents result
- Alternative of MongoDB operator $eq to get similar result
- How to implement MongoDB $or operator
- Using $addFields pipeline and run with the MongoDB $filter operator
- MongoDB pull with positional operator?
- Specify return format in MongoDB to return the values as an array?
- Retrieving group by result with arrays in MongoDB?
- Using positional operator with hierarchies in MongoDB?
- Format integer values with java.text.DecimalFormat
- MongoDB query to find on the basis of true or false values

Advertisements