
- 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 find on field combination of FirstName and LastName?
For combination, use $concat and check the equality using $eq. Let us create a collection with documents −
> db.demo502.insertOne({"FirstName":"John","LastName":"Smith"});{ "acknowledged" : true, "insertedId" : ObjectId("5e875534987b6e0e9d18f56d") } > db.demo502.insertOne({"FirstName":"David","LastName":"Miller"});{ "acknowledged" : true, "insertedId" : ObjectId("5e87553e987b6e0e9d18f56e") } > db.demo502.insertOne({"FirstName":"John","LastName":"Doe"});{ "acknowledged" : true, "insertedId" : ObjectId("5e875543987b6e0e9d18f56f") }
Display all documents from a collection with the help of find() method −
> db.demo502.find();
This will produce the following output −
{ "_id" : ObjectId("5e875534987b6e0e9d18f56d"), "FirstName" : "John", "LastName" : "Smith" } { "_id" : ObjectId("5e87553e987b6e0e9d18f56e"), "FirstName" : "David", "LastName" : "Miller" } { "_id" : ObjectId("5e875543987b6e0e9d18f56f"), "FirstName" : "John", "LastName" : "Doe" }
Following is the query to find on field combination −
> db.demo502.aggregate( ... [ ... { "$redact": { ... "$cond": [ ... { "$eq": [ ... { "$concat": [ "$FirstName", " ", "$LastName" ] }, ... "John Doe" ... ]}, ... "$$KEEP", ... "$$PRUNE" ... ] ... }} ... ] ... )
This will produce the following output −
{ "_id" : ObjectId("5e875543987b6e0e9d18f56f"), "FirstName" : "John", "LastName" : "Doe" }
- Related Articles
- MongoDB query to find documents with specific FirstName and LastName
- Sorting field value (FirstName) for MongoDB?
- MongoDB query to get distinct FirstName values from documents
- How to query on list field in MongoDB?
- MongoDB query to fetch only the “Name” field based on roles?
- Display specific name from a table with repeated individual FirstName and LastName using LIKE clause twice
- MongoDB query by sub-field?
- How to add a field with static value to MongoDB find query?
- MongoDB query to create new field and count set the count of another field in it?
- MongoDB query for a single field
- MongoDB query to update array with another field?
- MongoDB query to limit the returning values of a field?
- Query MongoDB with “like” implementation on name and email field beginning with a specific letter?
- MongoDB query to collectively match intersection of documents against a field
- MongoDB query to change simple field into an object?

Advertisements