
- 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
Find the exact match in array without using the $elemMatch operator in MongoDB?
As an alternative, use the $eq operator. Let us first create a collection with documents −
> db.equalDemo.insertOne({_id:1,"StudentFriendNames":["John","Carol","Sam"]}); { "acknowledged" : true, "insertedId" : 1 } > db.equalDemo.insertOne({_id:2,"StudentFriendNames":null}); { "acknowledged" : true, "insertedId" : 2 } > db.equalDemo.insertOne({_id:3,"StudentFriendNames":["Carol"]}); { "acknowledged" : true, "insertedId" : 3 } > db.equalDemo.insertOne({_id:4,"StudentFriendNames":["Sam"]}); { "acknowledged" : true, "insertedId" : 4 }
Following is the query to display all documents from a collection with the help of find() method −
> db.equalDemo.find();
This will produce the following output −
{ "_id" : 1, "StudentFriendNames" : [ "John", "Carol", "Sam" ] } { "_id" : 2, "StudentFriendNames" : null } { "_id" : 3, "StudentFriendNames" : [ "Carol" ] } { "_id" : 4, "StudentFriendNames" : [ "Sam" ] }
Following is the query to get the exact match using $eq −
> db.equalDemo.find({"StudentFriendNames":{$eq:["Carol"]}});
This will produce the following output −
{ "_id" : 3, "StudentFriendNames" : [ "Carol" ] }
- Related Articles
- How to find exact Array Match with values in different order using MongoDB?
- MongoDB $elemMatch to match document
- MongoDB query for exact match
- Write an equality in MongoDB without using $eq operator
- Find largest element from array without using conditional operator in C++
- MongoDB exact array matching
- Update field in exact element array in MongoDB?
- How can I use $elemMatch on first level array in MongoDB?
- Implement array match in MongoDB?
- MongoDB query for exact match on multiple document fields
- The Match Operator in Perl
- Match element in array of MongoDB?
- How do we find the exact positions of each match in Python regular expression?
- Program to find remainder without using modulo or % operator in C++
- Find XOR of two number without using XOR operator in C++

Advertisements