
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
MongoDB Query to Find a Specific City Record from a Collection
For this, use find() and fetch a specific record. Let us create a collection with documents −
> db.demo30.insertOne({"City":"New York"}); { "acknowledged" : true, "insertedId" : ObjectId("5e174ceccfb11e5c34d898bd") } > db.demo30.insertOne({"City":"Los Angeles"}); { "acknowledged" : true, "insertedId" : ObjectId("5e174d0ecfb11e5c34d898be") } > db.demo30.insertOne({"City":"Chicago"}); { "acknowledged" : true, "insertedId" : ObjectId("5e174d18cfb11e5c34d898bf") } > db.demo30.insertOne({"City":"Los Angeles"}); { "acknowledged" : true, "insertedId" : ObjectId("5e174d1dcfb11e5c34d898c0") }
Display all documents from a collection with the help of find() method −
> db.demo30.find();
This will produce the following output −
{ "_id" : ObjectId("5e174ceccfb11e5c34d898bd"), "City" : "New York" } { "_id" : ObjectId("5e174d0ecfb11e5c34d898be"), "City" : "Los Angeles" } { "_id" : ObjectId("5e174d18cfb11e5c34d898bf"), "City" : "Chicago" } { "_id" : ObjectId("5e174d1dcfb11e5c34d898c0"), "City" : "Los Angeles" }
Here is the query to find a specific city −
> db.demo30.find({"City":"Chicago"});
This will produce the following output −
{ "_id" : ObjectId("5e174d18cfb11e5c34d898bf"), "City" : "Chicago" }
Advertisements