
- 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 documents with specific FirstName and LastName
To find documents with specific FirstName and LastName, use $and along with $in. Implement this in MongoDB find(). Let us create a collection with documents −
> db.demo692.insertOne({FirstName:"Chris","LastName":"Brown"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea585dca7e81adc6a0b396a") } > db.demo692.insertOne({FirstName:"John","LastName":"Brown"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea585e2a7e81adc6a0b396b") } > db.demo692.insertOne({FirstName:"John","LastName":"Smith"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea585e7a7e81adc6a0b396c") } > db.demo692.insertOne({FirstName:"John","LastName":"Doe"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea585efa7e81adc6a0b396d") } > db.demo692.insertOne({FirstName:"Adam","LastName":"Smith"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea585faa7e81adc6a0b396e") }
Display all documents from a collection with the help of find() method −
> db.demo692.find();
This will produce the following output −
{ "_id" : ObjectId("5ea585dca7e81adc6a0b396a"), "FirstName" : "Chris", "LastName" : "Brown" } { "_id" : ObjectId("5ea585e2a7e81adc6a0b396b"), "FirstName" : "John", "LastName" : "Brown" } { "_id" : ObjectId("5ea585e7a7e81adc6a0b396c"), "FirstName" : "John", "LastName" : "Smith" } { "_id" : ObjectId("5ea585efa7e81adc6a0b396d"), "FirstName" : "John", "LastName" : "Doe" } { "_id" : ObjectId("5ea585faa7e81adc6a0b396e"), "FirstName" : "Adam", "LastName" : "Smith" }
Following is the query to find documents with specific FirstName and LastName −
> db.demo692.find({$and:[ {FirstName: {$in: ["Chris", "John"]}}, ... {LastName:{$in:["Brown", "Smith"]}} ... ] ... } ... );
This will produce the following output −
{ "_id" : ObjectId("5ea585dca7e81adc6a0b396a"), "FirstName" : "Chris", "LastName" : "Brown" } { "_id" : ObjectId("5ea585e2a7e81adc6a0b396b"), "FirstName" : "John", "LastName" : "Brown" } { "_id" : ObjectId("5ea585e7a7e81adc6a0b396c"), "FirstName" : "John", "LastName" : "Smith" }
- Related Articles
- MongoDB query to find on field combination of FirstName and LastName?
- MongoDB query to get distinct FirstName values from documents
- Display specific name from a table with repeated individual FirstName and LastName using LIKE clause twice
- MongoDB query to display documents with a specific name irrespective of case
- How to find MongoDB documents with a specific string?
- MongoDB query to update all documents matching specific IDs
- MongoDB query to match documents with array values greater than a specific value
- MongoDB query to implement nor query to fetch documents except a specific document
- MongoDB inverse of query to return all items except specific documents?
- MongoDB query to get only specific fields in nested array documents?
- MongoDB query to find matching documents given an array with values?
- MongoDB query to fetch a specific document rom documents with field value set using NumberInt()?
- Find MongoDB documents that contains specific field?
- MongoDB query to skip documents
- MongoDB aggregation to fetch documents with specific field value?

Advertisements